2013-07-08 35 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... 
}