2010-09-01 63 views
0

我想更新我的UITableView,下面的實現不工作。我想知道我是否做錯了什麼?UITableView不插入新行

NSDictionary *newContact = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Name", @"Phone", nil] forKeys: [NSArray arrayWithObjects:strName, strPhone, nil]]; 
[arrQuickDialContacts addObject:newContact]; 

NSLog(@"Returned %@ with phone %@\nNew Count: %d", strName, strPhone, [arrQuickDialContacts count]); 

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([arrQuickDialContacts count] - 1) inSection:0]; 
NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; 

[objQuickDialTableView beginUpdates]; 
[objQuickDialTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[objQuickDialTableView endUpdates]; 

這裏是我的UITableView的委託方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    // How many rows? 
    return [arrQuickDialContacts count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // See if we have any cells available for reuse 
    UITableViewCell *objCell = [tableView dequeueReusableCellWithIdentifier:@"QuickDialCell"]; 
    if (objCell == nil) { 

     // No reusable cell exists, so let's create a new one 
     objCell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"QuickDialCell"]; 
    } 

    // Give it data 
    NSDictionary *objRow = [arrQuickDialContacts objectAtIndex: [indexPath row]]; 
    objCell.textLabel.text = [objRow valueForKey:@"Name"]; 

    // Return the created cell 
    return objCell; 

} 

最後,我viewDidLoad中填充陣列的初始內容:提前

- (void)viewDidLoad { 

    // Load the quick dial contacts 
    NSString *strPlist = [[NSBundle mainBundle] pathForResource:@"QuickDialContacts" ofType:@"plist"]; 
    arrQuickDialContacts = [[NSMutableArray alloc] initWithContentsOfFile: strPlist]; 

    [super viewDidLoad]; 
} 

謝謝!

+0

類似的問題是不工作 – 2010-09-01 16:48:19

+0

表或表的創建此更新,當你運行它,會發生什麼? – 2010-09-01 19:44:06

回答

0

需要調用[tableView reloadData];

NOTE:事實上還有更多的細節

Unable to update UITableView

+0

我插入新行並更新表而不調用tableView reloadData。 [self.tableView insertRowsAtIndexPaths:@ [indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];這應該自動上傳表格。 – coolcool1994 2013-05-27 23:48:37