2013-10-29 60 views
5

我有一个包含UITableView的视图。此UITableView的单元格在Interface Builder中创建,以便具有不同类型的单元格。所以每个按钮的动作都在像下面这样的单元类中进行管理。使用自定义单元格获取UITableViewCell索引路径

- 我的包含不同类型的细胞的UITableView: - :

我类的用于 类型一个细胞 ( “CellTypeOne.h”)头文件

enter image description here

@interface CellTypeOne : UITableViewCell { } - (IBAction)actionForFirstButton:(id)sender; - (IBAction)actionForSecondButton:(id)sender; - (IBAction)actionForThirdButton:(id)sender; - (IBAction)actionForFourthButton:(id)sender; 

- 我的类的用于型两种细胞头文件( “CellTypeTwo.h”)

@interface CellTypeTwo : UITableViewCell 
{ 
} 

- (IBAction)actionForTheUniqueButton:(id)sender; 

- 含有我的表视图的视图(“ViewContainingMyTableView.h”) :

@interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource> 
{ 
    UITableView *myTBV; 
} 

@property (retain, nonatomic) IBOutlet UITableView *myTBV; 

@end 

在这里,我希望做的事:

例如,当我点击第一个单元格中的第一个按钮时,我希望能够显示“current”单元格的indexPath。

例如,我想有以下输出时:0

  • 我点击的第一个单元格的第三个按钮:

    • 我在第一个单元格点击第一个按钮: 0
    • 我点击第一个和唯一的按钮,在第二单元:1
    • 等...
  • +0

    您是否需要单击按钮上的单元格的indexpath? –

    +0

    是的,我需要包含单击按钮的单元格的indexPath。 –

    +0

    @Erzékiel只需在自定义单元格中放置一个委托,并且每次单击自定义单元格中的按钮时,都会触发delegate方法到viewcontroller,您可以将superview作为自定义单元格并获取所选单元格的索引路径 –

    回答

    10

    你正在使用自定义单元格,我认为你还需要处理选择,因为你正在触摸自定义单元格内的按钮而不是单元格本身,因此tableview的委托方法并没有被解雇,就像我在ur自定义单元格中所说的那样,把一个委托方法在自定义单元格中的示例

    CellTypeOne.h添加此

    //@class CellTypeOne; //if u want t pass cell to controller 
    @protocol TouchDelegateForCell1 <NSObject> //this delegate is fired each time you clicked the cell 
    - (void)touchedTheCell:(UIButton *)button; 
    //- (void) touchedTheCell:(CellTypeOne *)cell; //if u want t send entire cell this may give error add `@class CellTypeOne;` at the beginning 
    @end 
    
    @interface CellTypeOne : UITableViewCell 
    { 
    
    } 
    @property(nonatomic, assign)id<TouchDelegateForCell1> delegate; //defining the delegate 
    - (IBAction)actionForFirstButton:(id)sender; 
    - (IBAction)actionForSecondButton:(id)sender; 
    - (IBAction)actionForThirdButton:(id)sender; 
    - (IBAction)actionForFourthButton:(id)sender; 
    
    CellTypeOne.m文件

    @synthesize delegate; //synthesize the delegate 
    
    - (IBAction)actionForFirstButton:(UIButton *)sender 
    { 
        //add this condition to all the actions becz u need to get the index path of tapped cell contains the button 
        if([self.delegate respondsToSelector:@selector(touchedTheCell:)]) 
        { 
         [self.delegate touchedTheCell:sender]; 
         //or u can send the whole cell itself 
         //for example for passing the cell itself 
         //[self.delegate touchedTheCell:self]; //while at the defining the delegate u must change the sender type to - (void)touchedTheCell:(CellTypeOne *)myCell; if it shows any error in the defining of the delegate add "@class CellTypeOne;" above the defying the delegate 
        } 
    } 
    

    ,并在您ViewContainingMyTableView.h

    @interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource ,TouchDelegateForCell1> //confirms to custom delegate like table delegates 
    { 
        UITableView *myTBV; 
    } 
    
    @property (retain, nonatomic) IBOutlet UITableView *myTBV; 
    
    @end 
    

    ,并在ViewContainingMyTableView.m文件

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        //during the creating the custom cell 
        CellTypeOne *cell1 = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"]; 
        if(cell1 == nil) 
        { 
        cell1 = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
        } 
        cell.delegate = self; //should set the delegate to self otherwise delegate methods does not called this step is important 
    } 
    
    //now implement the delegate method , in this method u can get the indexpath of selected cell 
    
    - (void)touchedTheCell:(UIButton *)button 
    { 
        NSIndexPath *indexPath = [self.aTableView indexPathForCell:(UITableViewCell *)button.superview]; 
        NSLog(@"%@",indexPath.description); 
    
    } 
    /* if u pass the cell itself then the delegate method would be like below 
    - (void)touchedTheCell:(CellTypeOne *)myCell 
    { 
        NSIndexPath *indexPath = [self.aTableView indexPathForCell:myCell];//directly get the cell's index path 
        //now by using the tag or properties, whatever u can access the contents of the cell 
        UIButton *myButton = [myCell.contentView viewWithTag:1000]; //get the button 
        //... u can access all the contents in cell 
    } 
    */ 
    
    你的情况

    ,这是在细胞中第一个按钮,添加了具有不同功能的按钮,每一个委托方法,重复以上程序的另一个按钮在细胞

    希望u得到这个:) hapy编码

    +0

    我传入了touchedTheCell,所以这是一件好事。但'[self.tbvTimeLine indexPathForCell:(UITableViewCell *)button.superview]'为空... –

    +0

    你应该得到它正在为我工​​作的索引路径 –

    +0

    通过放置断点来检查 –

    0

    您可以在每个单元格的xib中使用标签来区分它们。 [self tag]细胞类中后

    使用

    +0

    单元格动态生成...我更喜欢不使用标签。 –

    1

    随着iOS7你可以这样说:

    - (IBAction)actionForTheUniqueButton:(id)sender 
    { 
        YouCellClass *clickedCell = (YouCellClass*)[[sender superview] superview]; 
        NSIndexPath *indexPathCell = [self.tableView indexPathForCell:clickedCell]; 
    } 
    
    +0

    哼,我该如何参考我的tableView?因为它在另一个视图中,所以'self.myTableView'不起作用... –

    +0

    它可能工作,但我不知道如何引用我的tableView.I试试这个,但它不起作用:'ViewContainingMyTableView * v = [[ViewContainingMyTableView alloc] init]; NSLog(@“myTBV:%@”,[p myTBV]);' –

    +0

    事实上...我认为你可以做的第一件事(因为你已经为单元格创建了自己的类)是添加一个属性在你的定制单元格引用表格视图显示单元格。这个解决方案是建议这个问题:http://stackoverflow.com/questions/1110482/reference-from-uitableviewcell-to-parent-uitableview – zbMax

    0

    在.H或.m文件创建的tableView IBOutlet中。您可以通过使用 UR actionForFirstButton内得到选择的TableCell的indexpath:方法

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    
    NSLog(@"I click on the first button in the first cell : %d", indexPath.row); 
    

    编辑:因为按钮的使用标签来获取索引路径,在您的actionForFirstButton方法

    if(sender.tag == 0 || 1 || 2 || 3){ 
        { 
         NSLog(@"the cell is : %d", 0); 
        } 
        else if(sender.tag == 4){ 
        { 
         NSLog(@"the cell is : %d", 1); 
        } 
        else{ 
        NSLog(@"the cell is : %d", 2); 
        } 
    
    +0

    我'不知道是否通过触摸单元格中的按钮,它选择了单元格... – zbMax

    +0

    当我单击按钮时,单元格未处于选定状态......这是我的问题! –

    +0

    我不想使用标签...对不起 –

    0

    您的需求就是要回一个按钮对应的行值...

    然后你有一个非常简单的方法,

    @interface CellTypeOne : UITableViewCell 
    { 
        NSNumber *rowval; 
    } 
    @property (nonatomic,retain)NSNumber* rowval; 
    
    在CellTypeTwo.h

    同样的事情

    @interface CellTypeTwo : UITableViewCell 
    { 
        NSNumber *rowval; 
    } 
    @property (nonatomic,retain)NSNumber* rowval; 
    

    ,目前在这两个.m文件您必须定义按钮的方法,因此,与这些方法 只是复制粘贴此行

    NSLog(@"Number : %d",[rowval integerValue]); 
    

    和内)的tableView的cellForRowAtIndexPath:方法中加入这一行太

    cell.rowval = [[NSNumber alloc]initWithInt:indexPath.row ]; 
    

    现在你按不同的CEL的一个按钮,每一次l它会回复单元格的行值

    希望这会有助于.. :-)

    相关问题