【问题标题】:How to do action using Button in UITableViewCell?如何在 UITableViewCell 中使用 Button 执行操作?
【发布时间】:2012-08-04 07:49:06
【问题描述】:

我有一个在我的 CustomCell 中创建的按钮。

- (IBAction)onoffBtn:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    if (button.selected)
    {
        // Do action 1
    }
    else
    {
        // Do action 2
    }
}

当我运行它时,按钮显示在我在 TableViewController 中创建的 UITableView 内的单元格中。我可以在我的牢房内按那个 onoffBtn,但它不能采取行动。如何让我的按钮执行操作?

这是我 TableViewController.m 中的 cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = _customCell;
        _customCell = nil;
    }
    // Show my cell
    return cell;
}

对于其他问题,我应该在哪里写做流程?在我的 TableViewController.m 或 CustomCell.m 中?如果你不介意,请给我代码和解释,我是新来的,还在学习。我想了解您编写的每一行代码。谢谢。

更新:

到目前为止,这是我在 TableViewController.m 中的 cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = _customCell;
        _customCell = nil;
    }
    button.tag = indexPath.row;
    cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
    cell.timerLabel.text =  [timeArray objectAtIndex:indexPath.row];
    time = [[secondArray objectAtIndex:indexPath.row] integerValue];
    NSLog(@"Title %@, time %@, second %@, time %i, tag %d", titleArray, timeArray, secondArray, time, button.tag);
    return cell;
}

这是日志说的:

2012-08-04 17:38:36.257 Table[1269:f803] Title (
    "Test 1",
    "Test 2"
), time (
    "10 secs",
    "5 secs"
), second (
    10,
    5
), time 10, tag 0 // tag 0 for cell 1
2012-08-04 17:38:36.261 Table[1269:f803] Title (
    "Test 1",
    "Test 2"
), time (
    "10 secs",
    "5 secs"
), second (
    10,
    5
), time 5, tag 0 // tag should be 1 for cell 2 but it said 0. How to fix it?

【问题讨论】:

    标签: iphone ios5 xcode4.3


    【解决方案1】:

    这就是你如何在单元格中添加带有动作的按钮

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    // ADDING A BUTTON WITH TARGET
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundRect];
    btn.frame = CGRectMake(0, 0, 50, 50);
    btn.showsTouchWhenHighlighted = YES;
    btn.tag = indexPath.row;
    [btn addTarget:self action:@selector(onoffBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.contentView addSubview:btn];
    
    return cell;
    }
    
    
    - (IBAction)onoffBtn:(id)sender
    {
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    if (button.selected)
    {
        // Do action 1
    }
    else
    {
        // Do action 2
    }
    }
    

    【讨论】:

    • 感谢您的快速回答,我是初学者。我能问点什么吗?我已经在我的 CustomCell 中放置了一个按钮,我应该使用您的代码再次添加一个按钮吗?非常感谢
    • 您可以删除您的按钮并添加此代码,这将根据框架集向所有单元格动态添加一个按钮,或者如果您的单元格是恒定的并且您通过 xib 自己添加了按钮,您可以简单地将相同的 IBAction 连接到 xib 文件中的所有按钮
    • 先生,我已经删除了它,但我可以将我的按钮与我的代码连接起来,然后我把 button.tag = indexPath.row;在我的 cellForRowAtIndexPath 和 NSLog 中。但是当我在表格视图中使用 2 个单元格运行它时,NSLog 显示的两个标记号都是 0,而不是 0 和 1。我忘记了什么吗?请教,如何让我的按钮可以设置正确的标签?
    • 你能告诉我你想要达到什么目标吗?我可以给你发一个演示,请不要叫我先生:)我也是一个学习者:)
    • 我的意思是我已经删除了 CustomCell.m 中的 UIButton 代码,但我已经将 CustomCell.xib 中的按钮连接到了 TableViewController.m 中的代码 onoffBtn 中,我还把 button.tag = indexPath.row;在我的 cellForRowAtIndexPath 和 NSLog 中。但是当我在表格视图中使用 2 个单元格运行它时,NSLog 显示的两个标记号都是 0,而不是 0 和 1。请教。我希望你能理解我的英语,因为英语不是我的母语。
    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 2016-03-26
    • 2017-09-23
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多