我希望我的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:
中使用了==
。虽然不是很重要,但它们之间应该保持一致。> =
将覆盖任何意外的双倍添加插入行,而==
将通过为可能导致该情况的任何代码错误引发异常来提供帮助。 - David Ravetti