要删除cell 首先要先删除数据源,然后在删除UI
类似于cell上有删除按钮,点击删除,在处理indexPath问题上要小心数组越界问题
如果在
tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath
里 deleteBtn.tag = indexPath.row;
对每一个cell上的 deleteBtn.tag只赋值一次
所以在在写删除函数时
- (void)deleteCell:(Button*)sender;
如果删除了数据源而不调用[tableView reloadData], deleteBtn.tag的值并未发生改变,所以在删除cell时会发生数组越界问题,所以如果用的tag值,一定要调用[tableView reloadData]刷表
如果在
- (void)deleteCell:(Button*)sender; 不想通过刷表来进行删除cell操作,就一定要取到被删除cell所在的indexPath.row
可以通过
[tableView beginUpdates];
NSInteger index = [tableView indexPathForCell:(UITableViewCell )[[sender superview] superview]].row;*
model = dataArray[index];
[dataArray removeObjectAtIndex:index];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];