gpt4 book ai didi

ios - 如何在自定义单元格中保存多个选定的按钮?

转载 作者:行者123 更新时间:2023-11-28 18:55:25 25 4
gpt4 key购买 nike

在我的自定义单元格中有很多问题和答案,在每个部分我想选择任何一个按钮,该按钮如何存储它?在 CellForRowAtIndexPath 中,我为每个答案创建了目标,这是我的代码!

-(void)buttonsClicked:(id)sender
{
UIButton *btn=(UIButton*)sender;

ContestQATableViewCell * cell=(ContestQATableViewCell *)[btn.superview superview];
if (cell.answer1.tag==btn.tag)
{
NSLog(@"%ld",(long)btn.tag);
[cell.answer1 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

[cell.answer1 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer2.tag==btn.tag)
{
NSLog(@"%ld",(long)btn.tag);
[cell.answer2 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

[cell.answer2 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer3.tag==btn.tag)
{
NSLog(@"%ld",(long)btn.tag);
[cell.answer3 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

[cell.answer3 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer4.tag==btn.tag)
{
NSLog(@"%ld",(long)btn.tag);
[cell.answer4 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

[cell.answer4 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
}
}

最佳答案

您需要在数据源数组中添加一个键,并根据单击操作时的按钮状态修改该键值。检查以下代码以供引用

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCustomTableViewCell *cell = (MyCustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCustomTableViewCell"];
[cell.btnSelect addTarget:self action:@selector(btnSelectClick:) forControlEvents:UIControlEventTouchUpInside];
cell.btnSelect.selected = [[[self.arraySource objectAtIndex:indexPath.row] valueForKey:@"isSleected"] boolValue];
cell.btnSelect.tag = indexPath.row;
return cell;
}
- (void)btnSelectClick:(UIButton *)sender {
sender.selected = !sender.selected;
NSMutableDictionary *dicTemp = [NSMutableDictionary dictionaryWithDictionary:[self.arraySource objectAtIndex:sender.tag]];
[dicTemp setValue:[NSNumber numberWithBool:sender.selected] forKey:@"isSleected"];
[self.arraySource replaceObjectAtIndex:sender.tag withObject:dicTemp];
}

Here is arraySource in view did load
self.arraySource = [NSMutableArray array];
[self.arraySource addObject:@{@"name":@"name-1"}];
[self.arraySource addObject:@{@"name":@"name-2"}];
[self.arraySource addObject:@{@"name":@"name-3"}];

关于ios - 如何在自定义单元格中保存多个选定的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34313642/

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