【发布时间】:2010-12-01 01:13:08
【问题描述】:
我希望我的 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:中的行数使用==。没什么大不了的,但它们之间应该保持一致。>=将涵盖插入行的任何意外双重添加,而==将通过为可能导致这种情况的任何代码错误抛出异常来提供帮助。
标签: objective-c iphone uitableview insert editing