繁体   English   中英

在自定义表格视图单元格中删除带有按钮的行

[英]delete row with button in custom tableview cell

这就是结构的样子。

--- UIView
   ---- ScrollView
       --- TableView

UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, -250, 320, 250)];

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, 320, 150) style:UITableViewStylePlain];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor blackColor];
    tableView.separatorStyle = normal;
    [tableView reloadData];

    [topView addSubview:tableView];

    [self.scrollView addSubview:topView];

在tableview中,我正在使用带有按钮的自定义tableview单元。 这是我的CellForRowAtIndex中按钮的代码

[cell.btnDelete addTarget:self action:@selector(deleteAppointment:) forControlEvents:UIControlEventTouchUpInside];

现在要获取特定行,请在我的deleteAppointment中执行此操作

 UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    NSLog(@"row: %d",indexPath.row);

但是它仍然给出以下错误。

-[ExceptionCell indexPathForCell:]: unrecognized selector sent to instance 0x210a2fa0

谁能帮我?

它真的很简单,位于cellForRowAtIndexPath 就像标记您的按钮

cell.button.tag = indexPath.row;

在按钮@selector方法中从数组或字典(即您的dataSource)中删除值

[array removeObjectAtIndex:button.tag];

现在重新加载tableView。

[tableView reloadData];

该错误表明您正在尝试在ExceptionCell上调用indexPathForCell indexPathForCellUITableView上的方法,而不是UITableViewCell上的方法。

cell.superview没有按预期返回UITableView

从TableView删除行时,您想要的是保留提供方法DeleteRowsAtIndexPath的直观动画,我一直在解决这个问题,并最终找到了EASY解决方案。

按照问题的要求,我们使用带有自定义UIButton的自定义单元格来删除该单元格

因此,您必须使用委托// CallBack。 下面的演示是针对C#中的MonoTouch,在目标C中是相同的,但是方法的命名方式与语法略有不同。

// TableViewController

//CALLBACK Methods WHEN DELETING A ROW
public void DeletedItem (MyObject arrayObject, CustomCell cellInstance)
    {
        NSIndexPath[] arrayPath = new NSIndexPath[1] { this.myTableView.IndexPathForCell (cellInstance) };

        this.myArray.RemoveItem (arrayObject);
        this.myTableView.DeleteRows (arrayPath, UITableViewRowAnimation.Fade);
    }


[Export("tableView:cellForRowAtIndexPath:")]
public virtual UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {

        ...

        cell.FactoryMethodsThatImUsing (myObject.items[indexPath.Row], DeletedItem);

        return cell;

    }

// CustomCell

public delegate void FOODelegateMethods (MyObject item, CustomCell instance);
public event FOODelegateMethods ItemDeleted;

在我的工厂方法中

if (!(this.ItemDeleted is Delegate)) {
            this.ItemDeleted += new CustomCell.FOODelegateMethods (ResultCallBack);
        }

然后在DeleteItem操作上

partial void DeleteItem (NSObject sender)
    {
        ItemDeleted(arrayObject, this);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM