如何从 mainVC.m 访问 customCell.m 中的变量

How to access variable in customCell.m from mainVC.m

我有一个自定义单元格,里面有一个按钮。有没有一种方法可以在按钮操作方法中访问从 mainVC.mcustomCell.m 的变量?

我可以这样做:mainVC main = [[mainVC alloc] init];,但这会创建一个新的主文件。我需要访问原件。

更新

我想做的是获取被单击的按钮所在单元格的索引路径。然后将该索引路径添加到在 mainVC.m.

中声明的 mutableArray

或者相反,如果可能的话,我想从 mainVC.m 到 heghtForRowAtIndexPath

的 class 中访问 customCell.m 中的变量]

一种有效的方法是在 mainVC 中定义委托协议,然后将 customCell 对象指定为委托。根据现在存在的对象之间的关系,您可能已经能够获得对现有 mainVC 的引用。老实说,我们需要说的代码比提议的那一行要多得多。

在您的单元格中添加一个 属性,它是指向您要使用的变量类型的指针。创建单元格时,将此 属性 设置为指向变量。我假设 mainVC.m 是带有 table 的控制器?

或者,将 属性 添加到单元格中,该单元格是 @property (nonatomic,copy) void (^onButtonAction)(UIButton *button) 形式的块,您可以在使用按钮时从单元格内部调用它。同样,您可以在创建单元格时分配它。该块在 table 视图控制器中定义,因此可以访问视图控制器的变量。

更好,为什么不避免使用按钮而只使用单元格选择。您可以通过实现来检测选择了哪一行:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

您可以在 cellForRowAtIndexPath 的按钮中设置标签 属性:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 ...

 cell.button.tag = indexPath.row;
 ...
}

那就开始行动吧

-(IBAction)buttonPressed:(id)sender{
    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", [button tag]);
}

当然这个索引应该是指你VC

上某个数组的索引

使用自定义单元格中的检查器为 UIButton 提供标签(例如,标签为 1)。然后根据 cellForRowAtIndexPath 中的标签创建按钮,然后将 indexpath.row 值指定为创建按钮的标签。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIButton *titleButton = (UIButton *)[cell viewWithTag:1];
    titleButton.tag = indexPath.row;
    [titleButton addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
}

按钮动作,可以使用

-(IBAction)btnSelected:(id)sender
{
   UIButton *btn = (UIButton *)sender;
   NSLog(@"%ld",(long)[btn tag]);
}

在按钮操作中,您将获得标签。您可以将这些标签用于数组以获得适当的值。

有很多方法可以从被点击的按钮中获取 indexPath 的单元格: 1.协议

// 在你的 customCell.h 文件中有一个委托方法

@protocol customCellDelegate <NSObject>

- (void)btn_ClickedForCell:(id)cell;

@end
@interface customCell : UITableViewCell
@property (nonatomic,assign)id<ChatOptionsPopUpDelegate>delegate;

// 在您的 customCell.m 文件中,单击按钮时调用此委托方法

- (void)btnAction{
  if ([_delegate respondsToSelector:@selector(btn_ClickedForCell:)])       {
        [_delegate btn_ClickedForCell:self];
    }
}

// 现在在你 mainVC.m 文件中分配这个委托

@interface mainVC () <customCellDelegate>

// in your `cellForRowAtIndexPath`
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 ...

 cell.delegate = self;
 ...
}

// 所以只要你的按钮被点击,函数就会被调用

- (void)btn_ClickedForCell:(id)cell{
      NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell];
     // Enjoy you got the indexPath
}
  1. 按标签

// 给你按钮标签,并通过标签获取它的单元格

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 ...

 cell.button.tag = indexPath.row;
 [cell.button addTarget:self action:@selector(btn_Clicked:) forControlEvents:UIControlEventTouchUpInside];
 ...
}

- (void)btn_Clicked:(UIButton*)sender{
         int tag = sender.tag;
          NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tag inSection:0];
       // Enjoy you got the tag
}