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

时间:2015-10-30 09:25:01

标签: ios iphone xcode button tableview

我对tableview有疑问。我有一个tableview和表格单元格创建按钮并设置标题NO。

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

enter image description here

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

1 个答案:

答案 0 :(得分:2)

尝试以下解决方案。

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