如何在iOS中pipe理tableview中的button?

我有问题与tableview。 我有一个tableview和在表单元格创buildbutton和设置标题号。

当用户触摸button的时候,设置button标题是。 但是如何知道标题YES中有多less个button。

在这里输入图像说明

请帮帮我………

尝试以下解决scheme

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

以下任何一种方式,您都可以更改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 } 

或点击button

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