如何在iOS中的TableView中管理按钮? [英] How to manage button inside tableview in ios?

查看:57
本文介绍了如何在iOS中的TableView中管理按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用tableview时遇到问题.我有一个tableview,并在表格单元格中创建按钮并设置标题NO.

I have problem with tableview. I have one tableview and in table cell create button and set title NO.

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

When user touch button at that time set button title YES. But how to know that how many button are there with title YES.

请帮助我......

Please help me.........

推荐答案

尝试以下解决方案.

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

以下两种方法中,您都可以更改按钮的状态,

Either of following ways, you can change state of your button,

// -------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中的TableView中管理按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆