我有一个 UIViewController,里面有一个带有 CustomCells 的 tableView,CustomCell 为一些单元格加载了一个 PLAY 按钮,这个按钮是在 中生成的自定义单元格.m
- (UIButton *)videoButton {
if (!videoButton) {
videoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
videoButton.frame = CGRectMake(120, 319, 50, 30);
[videoButton setTitle:@"Play" forState:UIControlStateNormal];
[videoButton addTarget:self action:@selector(PlayClicked:) forControlEvents:UIControlEventTouchUpInside];
videoButton.backgroundColor= [UIColor clearColor];
[self.contentView addSubview:videoButton];
}
playIMG.hidden = false;
return videoButton;
}
- (IBAction)PlayClicked:(id)sender {
TableView *tvvc = [[TableView alloc] init];
[tvvc PlayBtnClicked:[NSString stringWithFormat:@"%d", [sender tag]]];
}
在我的 TableView.m
- (IBAction)PlayBtnClicked:(id)sender {
NSString *tag = [NSString stringWithFormat:@"%@", sender]; //OK
int tagNumber = [tag intValue]; //OK
NSString *Media = [arrayID objectAtIndex:tagNumber]; //Not Getting Result !
NSLog(@"%@", arrayID); //Array is empty when logging it from this action
}
arrayID 是 NSMutableArray
arrayID = [[NSMutableArray alloc] init];
[arrayID addObject@"123"];
[arrayID addObject@"321"];
[arrayID addObject@"231"];
所以我通过在我的 TableView 中添加一个操作按钮来检查数组以记录数组并且它不为空。
如何修复实际上不为空的空数组?
最佳答案
我用比委托(delegate)更简单的方式解决了这个问题
已删除
[videoButton addTarget:self action:@selector(PlayClicked:) forControlEvents:UIControlEventTouchUpInside];
自定义单元格.m文件中的 Action
- (IBAction)PlayClicked:(id)sender {
TableView *tvvc = [[TableView alloc] init];
[tvvc PlayBtnClicked:[NSString stringWithFormat:@"%d", [sender tag]]];
}
在 tableView.m 中,我添加编辑了我想链接到此的操作
- (IBAction)PlayBtnClicked:(UIButton*)button {
NSLog(@"Button Clicked Tag: %d", button.tag);
}
在 cellForRowAtIndexPath 方法中,我将操作添加到自定义单元格中的按钮
if ([MediaType isEqualToString:@"video"]) {
cell.videoButton.hidden = NO;
cell.videoButton.tag = indexPath.row;
[cell.videoButton addTarget:self action:@selector(PlayBtnClicked:) forControlEvents:UIControlEventTouchUpInside];//here linking the action to the button
cell.playIMG.hidden = NO;
}
现在一切正常:)
关于ios - 从 customCell 按钮调用 TableView.m 中的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24812103/