UITableView Delete Row

前端 未结 2 861
孤城傲影
孤城傲影 2021-01-22 21:57

I am making a table view and I want to make a function where you can delete a row by swiping right and tapping the delete button. Me and my teacher have tried for about half an

2条回答
  •  野的像风
    2021-01-22 22:14

    This is because your numberOfRowsInSection data source implementation always returns 1 (fixed).

    Typically, you store objects in an array, which defines the number of rows. And, commitEditingStyle should remove the object from the array and then delete the row.

    – (void)tableView: (UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {
           // Delete the row from the data source
           [maTheData removeObjectAtIndex:[indexPath row]];
           // Delete row using the cool literal version of [NSArray arrayWithObject:indexPath]
    
           [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
        }
    }
    

    Follow this link for more details.

提交回复
热议问题