gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:23:52 24 4
gpt4 key购买 nike

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

当用户触摸按钮时设置按钮标题为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com