大致步骤:先通过按钮点击事件,获取当前cell所在位置,然后需要先将其数据模型删除,最后从tableview移除。
具体代码:
// 点击按钮事件
-(void)btnClick:(UIButton *)button{
UITableViewCell *cell = (UITableViewCell *)[button superview];
// 获取cell的indexPath
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSLog(@"点击的是第%ld行按钮",indexPath.row);
//消息框
NSString *name = [self.modelArray[indexPath.row] getName];
NSString *msg = [NSString stringWithFormat:@"已添加%@", name];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertController animated:true completion:nil];
// 从数组中移除指定数据,并更新UITableView
[self.modelArray removeObjectAtIndex:indexPath.row];
[self.tableView beginUpdates];
//NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
}