ios - 从 customCell 按钮调用 TableView.m 中的操作

标签 ios objective-c uitableview

我有一个 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
}

arrayIDNSMutableArray

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/

相关文章:

ios - 在 Xcode4 中使用外部构建的静态库

ios - iOS 中自动保存标签和开关信息

objective-c - 使用 Quartz 2D 解析 pdf 时获取文本位置

objective-c - 如何继承 UITableViewCell 的子类?

ios - 使用按钮展开/折叠 tableView 单元格,并在单元格展开时显示 View 中的其他元素

iOS:获取本周的所有日期

ios - 如何在标签栏 Controller 中嵌入 Split View Controller ?

objective-c - 如何以编程方式在 macOS 上查找挂载/弹出 USB 设备?

iphone - 如何确定 UISearchDisplayController 是否为 searchResults TableView 可见?

ios - 如何防止 UITableViewCell 在编辑模式下使用前导空格?