我正在尝试更新我的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];
}
提前致谢!