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

乔纳森·F。

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

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

在此处输入图片说明

-类一的单元格的头文件(“ 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
  • 等等...
Shankar BS

由于您正在使用自定义单元格,我认为您也需要处理选择,因为您触摸自定义单元格内的按钮而不是单元格本身,因此不会触发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 
 }
 */

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

获取嵌入在UITableViewCell中的UITable的单元格的indexPath

来自分类Dev

基于indexPath.row的自定义表单元格操作不起作用?

来自分类Dev

给定UITableViewCell的indexPath,加载该特定单元格

来自分类Dev

静态UITableViewCell中indexPath处的单元格重复

来自分类Dev

如何获取位于UICollectionView中心的单元格的indexPath

来自分类Dev

快速获取collectionView中心的单元格的indexPath?

来自分类Dev

iOS:UICollectionview,如何通过indexPath获取单元格的contentoffset

来自分类Dev

在单元格中获取 indexPath.row

来自分类Dev

如何知道单元格SWIFT的indexPath

来自分类Dev

表视图单元格中的UIGestureRecognizer获取indexPath始终返回最后一个单元格的indexPath

来自分类Dev

如何通过单击单元格的子视图获取UITableViewCell的indexPath-获取不兼容的指针类型警告

来自分类Dev

在UITableViewCell子类中获取indexPath

来自分类Dev

使用Swift在UITableViewCell中获取UITextField的indexPath

来自分类Dev

使用Swift在UITableViewCell中获取UITextField的indexPath

来自分类Dev

NSMutableArray使用IndexPath检查

来自分类Dev

使用自定义UITableViewCell中的按钮获取单元格字符串的问题

来自分类Dev

在indexpath的行单元格仅被调用一次

来自分类Dev

Swift:UICollectionView选择单元格indexPath问题

来自分类Dev

如何快速获得选定单元格的IndexPath

来自分类Dev

为什么我的UISearchDisplayController访问非法的单元格/ indexPath?

来自分类Dev

如何确定已轻按的开关的单元格的indexPath?

来自分类Dev

Xcode UICollectionView如何通过indexpath禁用某些单元格

来自分类Dev

关于表视图单元格indexpath.row

来自分类Dev

iOS:UICollectionview,如何通过它的indexPath获取单元格的contentoffset

来自分类Dev

如何从委托的收集视图单元格中获取indexPath项?

来自分类Dev

轻按单元格中的UIImageView时如何获取indexpath.row?

来自分类Dev

在单元格创建/出队期间获取 tableViewCell 文件中 tableView 的 indexPath

来自分类Dev

如何从Swift中的indexPath获取UITableViewCell对象?

来自分类Dev

UICollectionView在prepareForSegue中获取indexPath

Related 相关文章

  1. 1

    获取嵌入在UITableViewCell中的UITable的单元格的indexPath

  2. 2

    基于indexPath.row的自定义表单元格操作不起作用?

  3. 3

    给定UITableViewCell的indexPath,加载该特定单元格

  4. 4

    静态UITableViewCell中indexPath处的单元格重复

  5. 5

    如何获取位于UICollectionView中心的单元格的indexPath

  6. 6

    快速获取collectionView中心的单元格的indexPath?

  7. 7

    iOS:UICollectionview,如何通过indexPath获取单元格的contentoffset

  8. 8

    在单元格中获取 indexPath.row

  9. 9

    如何知道单元格SWIFT的indexPath

  10. 10

    表视图单元格中的UIGestureRecognizer获取indexPath始终返回最后一个单元格的indexPath

  11. 11

    如何通过单击单元格的子视图获取UITableViewCell的indexPath-获取不兼容的指针类型警告

  12. 12

    在UITableViewCell子类中获取indexPath

  13. 13

    使用Swift在UITableViewCell中获取UITextField的indexPath

  14. 14

    使用Swift在UITableViewCell中获取UITextField的indexPath

  15. 15

    NSMutableArray使用IndexPath检查

  16. 16

    使用自定义UITableViewCell中的按钮获取单元格字符串的问题

  17. 17

    在indexpath的行单元格仅被调用一次

  18. 18

    Swift:UICollectionView选择单元格indexPath问题

  19. 19

    如何快速获得选定单元格的IndexPath

  20. 20

    为什么我的UISearchDisplayController访问非法的单元格/ indexPath?

  21. 21

    如何确定已轻按的开关的单元格的indexPath?

  22. 22

    Xcode UICollectionView如何通过indexpath禁用某些单元格

  23. 23

    关于表视图单元格indexpath.row

  24. 24

    iOS:UICollectionview,如何通过它的indexPath获取单元格的contentoffset

  25. 25

    如何从委托的收集视图单元格中获取indexPath项?

  26. 26

    轻按单元格中的UIImageView时如何获取indexpath.row?

  27. 27

    在单元格创建/出队期间获取 tableViewCell 文件中 tableView 的 indexPath

  28. 28

    如何从Swift中的indexPath获取UITableViewCell对象?

  29. 29

    UICollectionView在prepareForSegue中获取indexPath

热门标签

归档