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

时间:2014-07-17 19:37:48

标签: ios objective-c uitableview

我有一个带有 tableView UIViewController ,其中包含CustomCells,CustomCell为某些单元格加载了一个PLAY按钮,此按钮在内生成CustomCell.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 中添加一个操作按钮来检查数组,以记录数组并且它不是空的。

如何修复实际上不为空的空数组?

2 个答案:

答案 0 :(得分:0)

我以比委托事物更容易的方式解决了问题

除去

[videoButton addTarget:self action:@selector(PlayClicked:) forControlEvents:UIControlEventTouchUpInside];

以及自定义单元格.m文件中的操作

- (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;
}

现在一切都运转良好:)

答案 1 :(得分:-1)

数组不是你的问题 - 你打破了表视图控制器及其单元的模型 - 视图 - 控制器性质。如果希望单元格中的控件调用表视图控制器中的方法,请使表视图控制器成为单元格的委托,并从单元格的按钮调用委托方法。

如果你发布的代码是正确的,那么你在PlayClicked方法中创建一个新的表视图实例,这绝对不是这样做的。

作为一个次要的风格点,方法命名的Cocoa约定是camelCase,带有小写的首字母。