[英]UITableView not inserting new row
我正在嘗試更新我的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];
}
提前致謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.