【发布时间】:2015-08-05 07:30:19
【问题描述】:
我在 UITableView Cell 上有两个单选按钮。我可以在向下滚动 UITableView 时选择其中一个,但是当我向上滚动 tableview 时,所有单选按钮都会被取消选中。滚动后,我也想让它们保持选中状态,但我无法做到这一点......所以请任何有解决方案的人帮助我。谢谢
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
leftBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtnclick.tag=999;
[leftBtnclick setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[leftBtnclick setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
if (screenHeight == 667)
{
[leftBtnclick setFrame:CGRectMake(50, 59, 30, 30)];
}
else if(screenHeight == 480)
{
[leftBtnclick setFrame:CGRectMake(50, 40, 30, 30)];
}
[leftBtnclick addTarget:self action:@selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.leftOptBtn addTarget:self action:@selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:leftBtnclick];
rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtnclick.tag=1000;
[rightBtnclick setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[rightBtnclick setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
if (screenHeight == 667)
{
[rightBtnclick setFrame:CGRectMake(180, 59, 30, 30)];
}
else if(screenHeight == 480)
{
[leftBtnclick setFrame:CGRectMake(50, 40, 30, 30)];
}
[rightBtnclick setFrame:CGRectMake(180, 59, 30, 30)];
[rightBtnclick addTarget:self action:@selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.rightOptBtn addTarget:self action:@selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:rightBtnclick];
cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row];
cell.questionLbl.text = [questionArray objectAtIndex:indexPath.row];
[cell.leftOptBtn setTitle:[leftOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
[cell.rightOptBtn setTitle:[rightOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
return cell;
}
-(void)leftTickBtnClicked:(id)sender
{
UIButton *leftTickBtn=(UIButton *)sender;
leftTickBtn.selected=!leftTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==1000)
{
UIButton *rightTickBtn=(UIButton *)vw;
if(leftTickBtn.selected)
{
rightTickBtn.selected=NO;
}
else
{
rightTickBtn.selected=YES;
}
}
}
}
-(void)rightTickBtnClicked:(id)sender
{
UIButton *rightTickBtn=(UIButton *)sender;
rightTickBtn.selected=!rightTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==999)
{
UIButton *leftTickBtn=(UIButton *)vw;
if(rightTickBtn.selected)
{
leftTickBtn.selected=NO;
}
else
{
leftTickBtn.selected=YES;
}
}
}
}
【问题讨论】:
-
这个问题有时会因重用 tableviewcell 而出现。删除
if语句并将NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil]; cell = [nib objectAtIndex:0];从if中写入这些行,然后重试。 -
还是不行......@SweetAngel
-
您的 tableview 中有多少行?
-
16 行 ...@SweetAngel