简体   繁体   English

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

[英]delete row with button in custom tableview cell

This is how the structure looks like. 这就是结构的样子。

--- 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];

In the tableview I am working with a custom tableview cell with a button in it. 在tableview中,我正在使用带有按钮的自定义tableview单元。 Here is the code for the button in my CellForRowAtIndex 这是我的CellForRowAtIndex中按钮的代码

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

Now to get the specific row I do this in my deleteAppointment 现在要获取特定行,请在我的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);

But it is still giving the following error. 但是它仍然给出以下错误。

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

Can anyone help me? 谁能帮我?

its really Simple, in cellForRowAtIndexPath . 它真的很简单,位于cellForRowAtIndexPath just tag your button like 就像标记您的按钮

cell.button.tag = indexPath.row;

remove the value from array or dictionary (thats your dataSource) in button @selector method 在按钮@selector方法中从数组或字典(即您的dataSource)中删除值

[array removeObjectAtIndex:button.tag];

Now reload the tableView. 现在重新加载tableView。

[tableView reloadData];

The error says you are trying to call indexPathForCell on ExceptionCell . 该错误表明您正在尝试在ExceptionCell上调用indexPathForCell indexPathForCell is a method on UITableView , not UITableViewCell . indexPathForCellUITableView上的方法,而不是UITableViewCell上的方法。

cell.superview is not returning your UITableView as you expect. cell.superview没有按预期返回UITableView

What you want when deleting a Row from a TableView is to keep the Intuitive Animation that gives the method DeleteRowsAtIndexPath, I have been around that problem, and finaly found an EASY solution. 从TableView删除行时,您想要的是保留提供方法DeleteRowsAtIndexPath的直观动画,我一直在解决这个问题,并最终找到了EASY解决方案。

As the question asked, we are using a Custom Cell with a Custom UIButton for Deleting the Cell 按照问题的要求,我们使用带有自定义UIButton的自定义单元格来删除该单元格

So, you'll have to use delegate // CallBack. 因此,您必须使用委托// CallBack。 That following demonstration is for MonoTouch in C#, it's the same in objective C, but methods are named a little bit differently + syntax. 下面的演示是针对C#中的MonoTouch,在目标C中是相同的,但是方法的命名方式与语法略有不同。

//TableViewController // 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 // CustomCell

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

IN MY Factory Method 在我的工厂方法中

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

And then on the DeleteItem Actions 然后在DeleteItem操作上

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

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

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