Ios 如何从mainVC.m访问customCell.m中的变量
我有一个自定义单元格,里面有一个按钮。在按钮操作方法中,是否有办法从Ios 如何从mainVC.m访问customCell.m中的变量,ios,objective-c,custom-cell,Ios,Objective C,Custom Cell,我有一个自定义单元格,里面有一个按钮。在按钮操作方法中,是否有办法从mainVC.m访问变量到customCell.m 我可以这样做:mainVC main=[[mainVC alloc]init],但这将生成一个新的主文件。我需要查阅原件 更新 我要做的是获取被单击的按钮所在单元格的索引路径。然后将该索引路径添加到在mainVC.m中声明的可变数组中 或者,相反,如果可能的话,我想从mainVC.m访问heghtforrowatinexpath类中的customCell.m中的变量。一种有效的
mainVC.m
访问变量到customCell.m
我可以这样做:mainVC main=[[mainVC alloc]init]代码>,但这将生成一个新的主文件。我需要查阅原件
更新
我要做的是获取被单击的按钮所在单元格的索引路径。然后将该索引路径添加到在mainVC.m中声明的可变数组中
或者,相反,如果可能的话,我想从mainVC.m访问heghtforrowatinexpath
类中的customCell.m中的变量。一种有效的方法是在mainVC中定义一个委托协议,然后将customCell对象指定为委托。根据当前存在的对象之间的关系,您可能已经能够获得对现有mainVC的引用。老实说,我们需要说的代码要比建议的那一行多得多。在单元格中添加一个属性,该属性是指向要使用的变量类型的指针。创建单元格时,将此属性设置为指向变量。我假设mainVC.m是包含表的控制器
或者,将一个属性添加到单元格中,该属性是使用按钮时从单元格内部调用的格式为@property(非原子,复制)void(^onButtonAction)(UIButton*button)
的块。同样,您可以在创建单元时分配该值。块在表视图控制器中定义,因此可以访问视图控制器的变量
更好的是,为什么不避免使用按钮,只使用单元格选择呢。您可以通过实现以下功能来检测选定的行:
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
您可以在CellForRowatineXpath中的按钮中设置标记属性:
- (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上某个数组的索引,在自定义单元格中使用inspector为UIButton提供标记(例如,标记为1)。然后基于CellForRowatineXpath中的标记创建按钮,然后将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;
//现在在您的mainVC.m
文件中分配此委托
@interface mainVC () <customCellDelegate>
// in your `cellForRowAtIndexPath`
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
cell.delegate = self;
...
}
按标签
//将标签交给按钮,并按标签获取其单元格
- (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
}
如何获取所选按钮的单元格?因为将有两个单独的操作。1-在didSelectRowAtIndexPath
中,以及2-如果我可以获得所选按钮的indexath单元格,我将不会使用按钮和单元格选择。如果需要UITableViewCell,请将其添加到块参数中:@property(非原子,复制)void(^onButtonAction)(UITableViewCell*inCell,UIButton*button)
。在块中,您现在有了单元格和按钮。@HotLicks这不在两个视图控制器之间。请注意,在第一个答案中:“使用Segue向前传递数据”或“向后传递数据”是否涉及我的问题,当没有向前或向后Segue时?好的,忽略讨论的用于类间通信的几种技术。@HotLicks没有找到我的答案。我不知道你在说什么。
- (void)btn_ClickedForCell:(id)cell{
NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell];
// Enjoy you got the indexPath
}
- (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
}