2013-07-08 37 views
0

中的自定義單元格的UIView控制器我有一個視圖控制器 在那裏有一個表格視圖,在一個自定義單元格。如何獲得包含在

我有一個自定義單元格

MyViewController 
---View 
------TableView 
-------------Custom cell 
-------------------UIButton 

我想實施的自定義單元格該按鈕的按鍵動作的按鈕,在自定義單元格類。

我想通過點擊按鈕泰德

-(IBAction)webButtonClicked:(id)sender 
{ 
    [self presentModalViewController:mailpage animated:YES]; 
} 

但這裏自我呈現稱爲mailPage另一viewcontoler意味着CustomCell,連我與上海華嘗試我沒有得到我的觀點對位指示代替自我

我試過這樣但沒用。

MyViewController *myViewController =self.superview 

如何獲得包含我查看CONTROLER當前自定義單元格

+0

您如何使用自定義單元格在表視圖?發佈代碼.... – Venkat

回答

4

我會強烈建議您將您的視圖控制器演示邏輯視圖控制器,而不是一個UITableViewCell

由於您已經在使用自定義單元格,因此這將非常簡單。只需爲您的自定義單元格定義一個新協議,並讓您的視圖控制器充當代表。或者,根據this answer here,您可以完全放棄委託,並且只需將您的視圖控制器作爲按鈕的目標。

您的自定義UITableViewCell真的不應該有任何依賴性或者它顯示在視圖控制器的知識。

2

以及最簡單的方法是在細胞和的cellForRowAtIndexPath方法來設置一個唯一的電子標籤的按鈕,你可以得到該按鈕實例作爲

UIButton *sampleButton=(UIButton *)[cell viewWithTag:3]; 

,並設置行動

[sampleButton addTarget:self action:@selector(sampleButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

,並設置在vi行動ewcontroller

-(void)sampleButtonPressed:(id)sender 
{ 
} 
2

試試這個:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"OpenHouseListCustomCell"; 
    OpenHouseListCustomCell *cell = (OpenHouseListCustomCell *)[tblOpenHouses dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) { 
     NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"OpenHouseListCustomCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
     cell.showsReorderControl = NO; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.backgroundColor=[UIColor clearColor]; 
     [cell.btn1 addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    } 

    cell.btn1.tag = indexpath.row; 
    return cell; 
} 

-(void) ButtonClicked { 
    //your code here... 
}