UITableView不插入新行

时间:2010-09-01 16:37:52

标签: iphone objective-c 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];
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

需要致电[tableView reloadData];

实际上有一个类似的问题,具体细节

Unable to update UITableView