这是我的问题,我在1个标签中有一个自定义单元格,在问题和答案中有4个按钮,当用户点击按钮时,其余3个按钮就像这样,
当用户滚动单元格时,它没有保存用户选择的按钮,我的意思是说用户点击它没有保存的按钮。 这是我的代码,
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];
}
}
答案 0 :(得分:2)
对于Check-Uncheck功能,仅buttonClicked:
方法是不够的。您还会在cellForRowAtIndexPath:
方法中选择选择按钮,或者取消选择,因为cellForRowAtIndexPath:
方法每次都会调用将滚动UITableView
,细胞将刷新。
例如
我一步一步解释
<强>步骤1 强>
创建两个数组,一个用于gloabally,另一个用于检查目的
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *arrayforCheckUnchek; // handle which button is selected or which is unselected
NSMutableArray *originalArray; // original Array
}
<强>步骤-2 强>
为数组分配内存
- (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"];
}
}
<强>步骤-2 强>
设置数据源方法
-(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;
}
<强>步骤-3 强>
这里选择的按钮索引可以更改图像
-(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"];
}
}