使用自定义单元格获取 UITableViewCell indexPath

Posted

技术标签:

【中文标题】使用自定义单元格获取 UITableViewCell indexPath【英文标题】:Get the UITableViewCell indexPath using custom cells 【发布时间】:2013-10-29 10:52:38 【问题描述】:

我有一个包含 UITableView 的视图。这个 UITableView 的单元格是在 Interface Builder 中创建的,以便拥有不同类型的单元格。因此,每个按钮的操作都在单元格类中进行管理,如下所示。

- 我的 UITableView 包含不同类型的单元格:

- type one 单元格 ("CellTypeOne.h") 的类的头文件:

@interface CellTypeOne : UITableViewCell



- (IBAction)actionForFirstButton:(id)sender;
- (IBAction)actionForSecondButton:(id)sender;
- (IBAction)actionForThirdButton:(id)sender;
- (IBAction)actionForFourthButton:(id)sender;

- type two 单元格 ("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

这是我想做的事情:

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

例如,我希望有以下输出时:

我点击第一个单元格中的第一个按钮:0 我点击第一个单元格中的第三个按钮:0 我点击第二个单元格中的第一个也是唯一的按钮:1 等等……

【问题讨论】:

点击按钮时是否需要单元格的索引路径? 是的,我需要包含单击按钮的单元格的 indexPath。 @Erzékiel 只需在自定义单元格中放置一个委托,每次单击自定义单元格中的按钮时都会触发委托方法到那里的 viewcontroller,您可以获得超级视图作为您的自定义单元格并获得索引所选单元格的路径 【参考方案1】:

由于您使用的是自定义单元格,我认为您还需要处理选择,因为您触摸的是自定义单元格内的按钮而不是单元格本身,因此不会触发 tableview 委托方法,正如我在自定义单元格中所说的那样更好一个委托方法,例如在你的自定义单元格中

在你的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 
 
 */

在您的情况下,这是单元格中的第一个按钮,为具有不同功能的每个按钮添加委托方法,对单元格中的其他按钮重复上述过程

希望你得到这个 :) 快乐编码

【讨论】:

我进入了touchedTheCell,所以这是件好事。但是[self.tbvTimeLine indexPathForCell:(UITableViewCell *)button.superview]是空的... self.myTBV 不为空,button.superview 也不为空。那么可能是什么问题呢? 检查你传递的单元格或按钮 哼...我不知道该怎么办:(button.superview等于&lt;UITableViewCellContentView: 0xa56e8a0; frame = (0 0; 320 110); opaque = NO; gestureRecognizers = &lt;NSArray: 0xa56e9f0&gt;; layer = &lt;CALayer: 0xa56e910&gt;&gt;(UITableViewCell *)button.superview也是... 让我们continue this discussion in chat【参考方案2】:

使用 ios7 你可以这样得到它:

- (IBAction)actionForTheUniqueButton:(id)sender

    YouCellClass *clickedCell = (YouCellClass*)[[sender superview] superview];
    NSIndexPath *indexPathCell = [self.tableView indexPathForCell:clickedCell];

【讨论】:

哼,怎么才能引用我的tableView呢?因为是另外一个视图,所以self.myTableView不起作用... 它可能有效,但我不知道如何引用我的 tableView。我尝试了这个,但它不起作用:ViewContainingMyTableView *v = [[ViewContainingMyTableView alloc] init]; NSLog(@"myTBV : %@", [p myTBV]); 事实上...我认为您可以做的第一件事(并且因为您已经为单元格创建了自己的类)是在自定义单元格中添加一个属性,该属性引用显示单元格的表格视图.建议针对此问题使用此解决方案:***.com/questions/1110482/…【参考方案3】:

您可以在每个单元格的 xib 中使用标签来区分它们。

在单元格类中[self tag]之后使用

【讨论】:

单元格是动态生成的...我宁愿不使用标签。【参考方案4】:

在 .h 或 .m 文件中创建 tableView IBOutlet。您可以通过使用获取所选表格单元的索引路径 在你的 actionForFirstButton: 方法中

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

【讨论】:

当我点击一个按钮时,单元格没有处于选中状态...这是我的问题! 我不想使用标签...对不起【参考方案5】:

您的要求只是取回按钮对应的行值...

那你有一个很简单的方法,

@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 ];

现在每次按下不同单元格的按钮时,它都会回复单元格的行值

希望这会有所帮助.. :-)

【讨论】:

以上是关于使用自定义单元格获取 UITableViewCell indexPath的主要内容,如果未能解决你的问题,请参考以下文章

在 UITableViewCell 中获取自定义单元格的高度

如何获取自定义单元格并通过 indexpath 使用它的自定义项 - swift3?

iPhone:如何从视图控制器中获取自定义单元格的按钮操作?

如何在 UIMenuController 的自定义操作中获取点击的表格视图单元格

带有自定义 textview 的 iphone tableview 单元格 - 获取 textview 参考

根据图像高度在集合视图自定义布局中获取单元格的高度