簡體   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