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

является это обновление таблицы или создания таблицы, не работает –

+0

Что происходит, когда вы запускаете его? –

ответ

0

нужно вызвать [tableView reloadData];

Acutally есть аналогичный вопрос с более специфического

Unable to update UITableView

+0

Я вставил новую строку и обновил таблицу без вызова tableView reloadData. [self.tableView insertRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationAutomatic]; Это должно автоматически загрузить таблицу. – coolcool1994

Смежные вопросы