使用滑动手势删除 tableview 上的行 [英] using swipe gesture to delete row on tableview
问题描述
我试图通过设置滑动手势,然后使用 IBAction 调用执行滑动的行的删除来绕过红色按钮删除 (editingstyledelete).应用程序崩溃,我收到以下错误:NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: 尝试从对象 [0] 插入 nil 对象
I am trying to bypass the red button delete (editingstyledelete) by setting up a gesture of swipe and then using IBAction to call a delete of the row that the swipe was performed in. The app crashes and I get a error of : NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]
- (IBAction)deleteSwipe:(id)sender{
[self deleteRow:self.tableView forCell:sender];
}
-(void) deleteRow:(UITableView *)tableView forCell:(UITableViewCell *)bCell{
NSIndexPath *indexPath = [tableView indexPathForCell:bCell];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.queue removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
我设置了一个断点异常,它指向:[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
并且在报告日志中它说索引路径值为 nil.之前用过'deleteRowsAtIndexPaths'方法,没发现这个问题.
i setup a breakpoint exception and it points to the: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
and in the report log it says that the index path value is nil. I have used the 'deleteRowsAtIndexPaths' method before and did not find this problem.
更新代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.queue removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView reloadData];
}
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
-(BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}
-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
推荐答案
你可以通过实现以下 UITableView 委托方法来做到这一点
You can do this by implementing following UITableView deligate methods
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone; //if you don't want to show delete button
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES; // allow that row to swipe
//return NO; // not allow that row to swipe
}
// Override to support editing/deleteing the table view cell on swipe.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleNone)
{
//--- your code for delete -----
}
}
这篇关于使用滑动手势删除 tableview 上的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!