简体   繁体   English

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

[英]Get the UITableViewCell indexPath using custom cells

I have a view which contain a UITableView. 我有一个包含UITableView的视图。 The cells of this UITableView are created in Interface Builder, in order to have different kinds of cells. 此UITableView的单元格在Interface Builder中创建,以便具有不同类型的单元格。 So the action of each button is managed in the cell classes like following. 因此,每个按钮的操作都在单元类中进行管理,如下所示。

- My UITableView containing different kinds of cells : - 我的UITableView包含不同类型的单元格:

在此输入图像描述

- The header file of my class for type one cells ("CellTypeOne.h") : - 我的类的头文件为类型一单元格(“CellTypeOne.h”)

@interface CellTypeOne : UITableViewCell
{
}

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

- The header file of my class for type two cells ("CellTypeTwo.h") : - 我的类的头文件,用于类型二单元格(“CellTypeTwo.h”)

@interface CellTypeTwo : UITableViewCell
{
}

- (IBAction)actionForTheUniqueButton:(id)sender;

- The view which contain my table view ( "ViewContainingMyTableView.h" ) : - 包含我的表视图的视图( “ViewContainingMyTableView.h” ):

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

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

@end

Here the thing I want to do : 这是我想要做的事情:

When I click on the first button in the first cell, for example, I want to be able to show the indexPath of the "current" cell. 例如,当我单击第一个单元格中的第一个按钮时,我希望能够显示“当前”单元格的indexPath。

For example , I want to have the following output when : 例如 ,我希望在以下情况下输出以下内容:

  • I click on the first button in the first cell : 0 我点击第一个单元格中的第一个按钮: 0
  • I click on the third button in the first cell : 0 我点击第一个单元格中的第三个按钮: 0
  • I click on the first and unique button in the second cell : 1 我点击第二个单元格中的第一个和唯一按钮: 1
  • etc... 等等...

since u are using the custom cell i think u need to handle selection also, because u are touching the button inside the custom cell not the cell itself therefore tableview delegate methods are not fired, better as i said in ur custom cell put a delegate method for example in ur custom cells 因为你正在使用自定义单元格我认为你还需要处理选择,因为你正在触摸自定义单元格内的按钮而不是单元格本身,因此tableview委托方法没有被触发,更好,因为我在你的自定义单元格中说了一个委托方法例如在你的自定义单元格中

in your CellTypeOne.h add this 在你的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;

in your CellTypeOne.m file 在您的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
    }
}

and in your ViewContainingMyTableView.h 并在您的ViewContainingMyTableView.h

 @interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource ,TouchDelegateForCell1> //confirms to custom delegate like table  delegates
 {
    UITableView *myTBV;
 }

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

 @end

and in the ViewContainingMyTableView.m file 并在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 
 }
 */

in your case, this is for first button in the cell, add the delegate methods for each buttons having different functions, repeat above procedure for another buttons in the cell 在你的情况下,这是单元格中的第一个按钮,为每个具有不同功能的按钮添加委托方法,对单元格中的另一个按钮重复上述过程

hope u get this :) hapy coding 希望你得到这个:) hapy编码

With iOS7 you can get it like this : 使用iOS7,您可以这样:

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

You may use tag inside your xib on each cell to differentiate them. 您可以在每个单元格的xib中使用标记来区分它们。

Use after [self tag] inside the cell class 在单元格类中的[self tag]之后使用

create tableView IBOutlet in .h or .m file. 在.h或.m文件中创建tableView IBOutlet。 You can get the indexpath of selected tablecell by using inside ur actionForFirstButton: method 您可以使用内部的actionForFirstButton:方法获取所选表格单元的索引路径

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

 NSLog(@"I click on the first button in the first cell : %d", indexPath.row);

edited : use tag of the button to get the index path , in your actionForFirstButton method 编辑 :在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);
    }

Your requirement is just to get back the corresponding row value of a button... 您的要求只是获取按钮的相应行值...

Then you have a very simple way , 然后你有一个非常简单的方法,

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

same thing in your CellTypeTwo.h 你的CellTypeTwo.h中也是一样的

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

and now in both .m file you must have defined a method for buttons, So with in those method just paste this line 现在在.m文件中你必须为按钮定义一个方法,所以在这些方法中只需粘贴这一行

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

and within )tableView cellForRowAtIndexPath: method add this line too 在tableView cellForRowAtIndexPath:方法中也添加此行

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

Now every time you press a button of different cell it will reply back with row value of the cell 现在,每当您按下不同单元格的按钮时,它将回复单元格的行值

Hope this will help.. :-) 希望这会有所帮助.. :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM