ios - 如何在ios中管理tableview内的按钮?

标签 ios iphone xcode button tableview

我的表格 View 有问题。我有一个表格 View ,在表格单元格中创建按钮并设置标题号。

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

enter image description here

请帮帮我......

最佳答案

尝试以下解决方案。

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

关于ios - 如何在ios中管理tableview内的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33432578/

相关文章:

ios - 使 UITextView 父级成为它自己的 inputAccessoryView

ios - 如何使用 ENUM 作为方法的参数

iphone - 来自相机的原始图像数据

iphone - 自定义UIPickerView和UIDatePicker,扁平化设计

ios - rsync 错误 : some files could not be transferred (code 23) Command PhaseScriptExecution failed with a nonzero exit code

ios - iOS10 中 isKindOfClass 面临的问题

iOS 类似 Mac 应用程序中的坐标系统

iphone - 让 View Controller 更改等待自定义动画结束?

iphone - xcode: 属性 'title' 'copy' 属性与父类(super class) 'UIViewController' 属性不匹配

objective-c - 最前面的窗口使用 CGWindowListCopyWindowInfo