【发布时间】:2009-03-26 15:05:26
【问题描述】:
我有一个 tableView 需要在从另一个视图插入信息后更新。如果我执行一个
[self.tableView reloadData];
下一次我在另一个视图中插入更多信息并尝试重新加载表时,所有当前可见的行都会重复。
换句话说,当我启动应用程序时,我拥有:
tableView:
Row 1
Row 2
然后我提交了一些信息,这些信息也会显示在表格中,然后我突然有了:
tableView
Row 1
Row 2
Row 3 <- info I just added
Row 1
Row 2
我的 numberOfRowsInSection 实现如下所示:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [ItemsController sharedItemsController].count;
}
我的 cellForRowAtIndexPath 实现如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ItemsController* controller = [ItemsController sharedItemsController];
NSMutableArray* recentItems = controller.listOfRecentItems;
CustomCell *cell = nil;
NSUInteger row = [indexPath row];
if( row < recentItems.count )
{
Items* item = [recentItems objectAtIndex:row];
if( recentCellData == nil )
recentCellData = [[NSMutableDictionary alloc] initWithCapacity:[indexPath length]];
if( [recentCellData count] > 0 )
cell = [recentCellData objectForKey:[NSString stringWithFormat:@"%d", row]];
if (cell == nil) {
UIViewController * view1 = [[UIViewController alloc] initWithNibName:@"CustomCell" bundle:nil];
cell = (CustomCell*)[view1 view];
[recentCellData setObject:cell forKey:[NSString stringWithFormat:@"%d",row]];
}
// do some other stuff here
}
// Set up the cell
return cell;
}
更新表并避免重复当前可见行的最佳方法是什么。 提前感谢所有帮助!
【问题讨论】:
-
似乎您需要在问题中包含 tableView:numberOfRowsInSection: 方法的实现。
-
我将该信息添加到问题中。谢谢!
标签: iphone uitableview sdk