2012-12-04 42 views
0

這就是結構的外觀。用自定義表格單元格中的按鈕刪除行

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

任何人都可以幫我嗎?

+0

你應該看看我的解決方案 – Marc

回答

6

其確實很簡單,在cellForRowAtIndexPath。只是標記您按鈕等

cell.button.tag = indexPath.row; 

在按鈕@selector方法從陣列或字典(這就是你的數據源)刪除值

[array removeObjectAtIndex:button.tag]; 

現在重新加載的tableView。

[tableView reloadData]; 
+0

使用該解決方案,您將無法使用TableView方法DeleteRows爲您提供的直觀動畫! – Marc

+0

你會得到動畫,試試這個。它將首先執行動畫,然後重新加載表以更新索引。 – junaidsidhu

0

錯誤表示您正嘗試致電indexPathForCellExceptionCellindexPathForCellUITableView上的方法,而不是UITableViewCell

cell.superview不像您期望的那樣返回您的UITableView

+0

是的,我認爲這是因爲我把我的桌面視圖到我的scrollview?這可能嗎 ? – Steaphann

0

刪除從TableView中一排,當你需要的是保持直觀的動畫,使該方法DeleteRowsAtIndexPath,我一直在圍繞着問題,finaly發現了一個簡單的解決方案。

當問問題,我們使用的是有一個自定義的UIButton自定義單元格刪除單元格

所以,你必須使用委託//回調。 下面的示例適用於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); 
    } 
相關問題