我希望我的UITableView的行为与联系人编辑器中的表格类似,即用户应点击编辑,并在每个部分的底部显示“添加新类别”行。在UITableView中使用插入行
我使用下面的代码来做到这一点,但问题是没有平滑的过渡,因为有联系人。相反,新的行突然出现。我如何获得动画?
另外,如何响应点击“添加新类别”行?该行在我当前的实现中不可点击。
用户开始编辑时是否需要重新加载数据?我这样做是因为否则插入行从不绘制。
谢谢。
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
[tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section {
// ...
if(self.tableView.editing)
return 1 + rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// .....
NSArray* items = ...;
if(indexPath.row >= [items count]) {
cell.textLabel.text = @"add new category";
}
// ...
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray* items = ...;
if(indexPath.row == [items count])
return UITableViewCellEditingStyleInsert;
return UITableViewCellEditingStyleDelete;
}
这是非常有益的(有答案一起,下同)。只是一个小的不一致性 - 与'tableView:cellForRowAtIndexPath:'中的行数比较'使用'> ='而'tableView:editingStyleForRowAtIndexPath:'中的使用'=='。没什么大不了的,但是它们之间应该是一致的。 '> ='将覆盖任何意外的双重添加插入行,而'=='将通过抛出一个异常来解决任何可能导致这种情况的代码错误。 – 2013-01-21 20:38:15