gpt4 book ai didi

ios - 如何在自定义单元格中保存单击的按钮索引路径?

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

这是我的问题,我在 1 个标签和 4 个按钮(如问题和答案)中有一个自定义单元格,当用户单击按钮 1 时,剩下的 3 个按钮如下所示, enter image description here

当用户滚动单元格时,它没有保存用户选择的按钮,我的意思是说用户点击了它没有保存的按钮。这是我的代码,

ViewDidLoad()
{
testArray = [[NSMutableArray alloc]init];
for (int i =0; i<[mainArray count]; i++)
{
[testArray addObject:@"Unchecked"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"Cell";
ContestQATableViewCell *cell =(ContestQATableViewCell *)[tableViewQA dequeueReusableCellWithIdentifier:cellId];
if (cell==nil)
{
NSArray *myNib;
myNib =[[NSBundle mainBundle]loadNibNamed:@"ContestQATableViewCell" owner:self options:nil];
cell = (ContestQATableViewCell *)[myNib lastObject];
}
cell.textLabel.text = [mainArray objectAtIndex:indexPath.row];
if([[testArray objectAtIndex:indexPath.row] isEqualToString:@"Unchecked"])
[cell.answer1 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
else
[cell.answer1 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
cell.answer1.tag = indexPath.row;
[cell.answer1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:cell.answer1];

return cell;

[cell.answer1 addTarget:self action:@selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer2 addTarget:self action:@selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer3 addTarget:self action:@selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer4 addTarget:self action:@selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

这是我的添加目标方法

-(void)buttonsClicked:(id)sender
{
UIButton *btn=(UIButton *)sender;
ContestQATableViewCell * cell=(ContestQATableViewCell *) [btn.superview superview];
if (cell.answer1.tag==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)
{
[cell.answer2 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

[cell.answer2 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer3.tag==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)
{
[cell.answer4 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

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

最佳答案

对于 Check-Uncheck 功能,只有 buttonClicked: 方法是不够的。您还将在 cellForRowAtIndexPath: 方法中为哪个按钮被选中 或哪个按钮处于未选中 中添加条件,因为 cellForRowAtIndexPath: 每次滚动 UITableView 时都会调用方法,并且单元格会刷新。

例如

一步一步解释

第一步

Create the two arrays one for gloabally another one for checking purpose

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{

NSMutableArray *arrayforCheckUnchek; // handle which button is selected or which is unselected
NSMutableArray *originalArray; // original Array
}

第二步

allocating the memory for arrays

- (void)viewDidLoad
{
[super viewDidLoad];


arrayforCheckUnchek = [[NSMutableArray alloc]init];

originalArray = [[NSMutableArray alloc]initWithObjects:@"cell1",@"cell2",@"cell3",@"cell4",@"cell5", nil];

// setting all cell initilayy at un check
for(int i=0; i<[originalArray count]; i++)
{
[arrayforCheckUnchek addObject:@"Unchecked"];
}

}

第二步

setup your datasource Methods

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [originalArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [originalArray objectAtIndex:indexPath.row];

UIButton *radiobutton = [UIButton buttonWithType:UIButtonTypeCustom];
[radiobutton setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)]; // customize the frames

if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Unchecked"])
[radiobutton setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
else
[radiobutton setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
radiobutton.tag = indexPath.row;
[radiobutton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:radiobutton];

return cell;
}

第三步

here selected button index you can change the image

 -(void)buttonClicked:(UIButton *)sender
{
//Getting the indexPath of cell of clicked button

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:yourtableviewname];
NSIndexPath *indexPath = [yourtableviewname indexPathForRowAtPoint:touchPoint];

//Checking the condition button is checked or unchecked.

if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Unchecked"])
{
[sender setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
[arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Checked"];
}
else
{
[sender setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
[arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Unchecked"];
}
}

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

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