我有UITableView有三个部分。我在每个部分的每个单元格中都有一个单选按钮。我需要一次在每个部分中选择一个单选按钮。这意味着我需要选择section1中的3rd cells单选按钮和下一节中的1st cells单选按钮,依此类推。现在的问题是,如果我在一个部分中选择一个单选按钮,如果我尝试在其他部分中选择其他单元格,则先前选择的单选按钮将替换为新选择的单选按钮。我将单选按钮添加为单元accessoryView。请帮帮我。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"nib" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.my_label.text=@"";
cell.labelKWD.hidden=TRUE;
UIButton *newRadioButton;
newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:@"tick_button_b.png"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:@"tick_button_a.png"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
if ([indexPath isEqual:selectedIndex]) {
newRadioButton.selected = YES;
}
else {
newRadioButton.selected = NO;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath;
[tableView reloadData];
}
答案 0 :(得分:0)
您需要为所选的indexPaths设置NSMutableArray
。那么你的didSelectRowAtIndexPath:
会是这样的。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if([self.selectedIndexArray containsObject:indexPath]){
[self.selectedIndexArray removeObject:indexPath];
}
else{
[self.selectedIndexArray addObject:indexPath];
}
[tableView reloadData];
}
您更改选择的和cellForRowAtIndexPath:
将是这样的:
if([self.selectedIndexArray containsObject:indexPath]){
newRadioButton.selected = YES;
}
else{
newRadioButton.selected = NO;
}