2010-09-01 68 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