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

5
我有一个视图包含一个UITableView。这个UITableView的单元格是在Interface Builder中创建的,以便拥有不同类型的单元格。因此,每个按钮的操作都由单元格类管理,如下所示。
- 我的UITableView包含不同类型的单元格: enter image description here - 我的类型一单元格类的头文件("CellTypeOne.h"):
@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

我想做的事情是:

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

例如,当我:

  • 点击第一个单元格中的第一个按钮时,我希望输出:0
  • 点击第一个单元格中的第三个按钮时,我希望输出:0
  • 点击第二个单元格中唯一的按钮时,我希望输出:1
  • 等等...

你需要在点击按钮时获取单元格的indexPath吗? - Jay Gajjar
是的,我需要包含点击按钮的单元格的indexPath。 - Jonathan F.
@Erzékiel,简单来说,在自定义单元格中放置一个委托,并在每次单击自定义单元格中的按钮时触发委托方法到视图控制器。在那里,您可以获取父视图作为您的自定义单元格,并获取所选单元格的索引路径。 - Shankar BS
5个回答

10

由于您正在使用自定义单元格,我认为您需要处理选择操作,因为您正在触摸自定义单元格内的按钮而不是单元格本身,因此表视图委托方法不会被触发。最好在您的自定义单元格中添加一个委托方法,例如在您的自定义单元格中。

在您的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]为空... - Jonathan F.
self.myTBV 不为 null,而且 button.superview 也不为 null。那么问题可能是什么? - Jonathan F.
检查您传递的是单元格还是按钮。 - Shankar BS
哎呀...我不知道该怎么办 :( button.superview 等于 <UITableViewCellContentView: 0xa56e8a0; frame = (0 0; 320 110); opaque = NO; gestureRecognizers = <NSArray: 0xa56e9f0>; layer = <CALayer: 0xa56e910>>(UITableViewCell *)button.superview 也是这样... - Jonathan F.
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/40183/discussion-between-shan-and-erzekiel - Shankar BS

1
使用iOS7,可以像这样获得它:
- (IBAction)actionForTheUniqueButton:(id)sender
{
    YouCellClass *clickedCell = (YouCellClass*)[[sender superview] superview];
    NSIndexPath *indexPathCell = [self.tableView indexPathForCell:clickedCell];
}

哎呀,我该如何引用我的tableView?因为它在另一个视图中,所以 self.myTableView 不起作用... - Jonathan F.
它可能会起作用,但我不知道如何引用我的tableView。我尝试了这个,但它不起作用:ViewContainingMyTableView *v = [[ViewContainingMyTableView alloc] init]; NSLog(@"myTBV : %@", [p myTBV]); - Jonathan F.
事实上... 我认为你可以做的第一件事(因为你已经为单元格创建了自己的类)是在自定义单元格中添加一个属性,该属性指向显示该单元格的表视图。这个解决方案建议在这个问题中使用:https://dev59.com/HHNA5IYBdhLWcg3wC5Xh - zbMax

0

在.h或.m文件中创建tableView IBOutlet。您可以通过在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);
    }

当我点击按钮时,单元格没有处于选定状态...这就是我的问题! - Jonathan F.
我不想使用标签...抱歉。 - Jonathan F.

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

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

希望这能帮到您.. :-)


0

您可以在xib中为每个单元格使用tag进行区分。

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


单元格是动态生成的...我更喜欢不使用标签。 - Jonathan F.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接