【问题标题】:How to manage button inside tableview in ios?如何在ios中管理tableview中的按钮?
【发布时间】:2016-01-30 16:15:56
【问题描述】:

我对 tableview 有问题。我有一个表格视图,在表格单元格中创建按钮并设置标题 NO。

当用户触摸按钮时,设置按钮标题是。但是如何知道有多少个标题为 YES 的按钮。

请帮帮我.........

【问题讨论】:

  • 存储名称为YES的按钮的索引路径
  • 您可以创建一个NSMutableDictionary,键为 indexPath.row,值为是或否。每次调用按钮的IBAction 时更新您的字典。
  • 请发布您已经尝试过的代码示例
  • @NSNoob NSMutableIndexSet 是一种比字典更好的数据结构
  • @Paulw11 我不知道。如你所见,这里是 NSNoob。

标签: ios iphone xcode button tableview


【解决方案1】:

尝试以下解决方案。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    ObjectClass *obj = [yourArray objectAtIndex:indexPath.row];
    cell.textLabel.text =  obj.someTextPropertyValue;

    // ----- If you have custom cell, then omit this part -----
    UIButton *btnYesNo = [UIButton buttonWithType:UIButtonTypeCustom];
    btnYesNo.frame = CGRectMake(290, 0, 30, 30);
    [btnYesNo setImage:[UIImage imageNamed:@"plus_img.png"] forState:UIControlStateNormal];
    [btnYesNo setImage:[UIImage imageNamed:@"plus_img.png"] forState:UIControlStateSelected];
    [btnYesNo setTitle:@"No" forState:UIControlStateNormal];
    [btnYesNo setTitle:@"Yes" forState:UIControlStateSelected];
    [cell.contentView addSubview:btnYesNo];
    // --------------------

    btnYesNo.selected = obj.selected; // At first, this selected property of type add to your object class
    btnYesNo.tag = indexPath.row;
    [btnYesNo addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

以下任一方式,您都可以更改按钮的状态,

// -------First way --> Either change state on select row --------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // ----- If you have custom cell, then omit this part -----
    for (id view in cell.contentView.subviews)
    {
        if ([view isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)view;
            if (btn.selected)
            {
               btn.selected = FALSE;
            }
            else
            {
               btn.selected = TRUE;
            }
        }
    }
    // ------------------------
    // ----- Have custom cell, then directly use----
    if (cell.btnYesNo.selected)
    {
        cell.btnYesNo.selected = FALSE;
    }
    else
    {
        cell.btnYesNo.selected = TRUE;
    }
     //-----------------------------------------

    ObjectClass *obj = [yourArray objectAtIndex:indexPath.row];
    obj.selected = sender.selected;
    // Call web service to update status Or update recoed in database
}

或者点击按钮

// ---------- Or you want to change state only on button click -------
- (void)changeState:(UIButton *)sender
{
    if (sender.selected)
    {
        sender.selected = FALSE;
    }
    else
    {
        sender.selected = TRUE;
    }

    ObjectClass *obj = [yourArray objectAtIndex:sender.tag];
    obj.selected = sender.selected;
    // Call web service to update status Or update recoed in database
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多