sna*_*ish 2 objective-c uitableview nsindexpath xcode5
我试图通过设置滑动手势,然后使用IBAction调用删除执行滑动的行来绕过红色按钮删除(editingstyledelete).应用程序崩溃,我得到一个错误: 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];
}
Run Code Online (Sandbox Code Playgroud)
我设置了断点异常并指向:[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];在报告日志中它表示索引路径值为nil.我之前使用过'deleteRowsAtIndexPaths'方法,但没有发现这个问题.
编辑:更新的代码:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
您可以通过实现以下UITableView deligate方法来完成此操作
- (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 -----
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3331 次 |
| 最近记录: |