目标 c 中的单选按钮问题

纳比尔

我有一个表视图,我正在加载一些数据,我试图在该数据上制作单选按钮,但是当我选择任何单元格时,它不会取消选择其他单元格,它作为一个复选框工作。我很困惑我在哪里做错了,我的代码是这样的,

-(UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *cellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
    radiobtn.frame = CGRectMake(30, 60, 30, 30);

    [radiobtn setImage:[UIImage imageNamed:@"circle.png"] forState:UIControlStateNormal];
    [radiobtn setImage:[UIImage imageNamed:@"rights.png"] forState:UIControlStateSelected];
    [radiobtn addTarget:self action:@selector(radiobtn:)
       forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryView = radiobtn;

}

cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(0) green:(0) blue:(0) alpha:1];
cell.selectedBackgroundView = selectionColor;
cell.selectionStyle = UITableViewCellDragStateLifting;
return cell;

}

-(void)radiobtn:(UIButton *)sender
{
if([sender isSelected])
{
    [sender setSelected:NO];
}

else
{
    [sender setSelected:YES];
}
}

它看起来像这样, 图像

马丁

试试这个。

-(UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *cellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    UIButton *radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
    radiobtn.frame = CGRectMake(30, 60, 30, 30);
    radiobtn.userInteractionEnabled = NO;
    [radiobtn setImage:[UIImage imageNamed:@"circle.png"] forState:UIControlStateNormal];
    [radiobtn setImage:[UIImage imageNamed:@"rights.png"] forState:UIControlStateSelected];
//    [radiobtn addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryView = radiobtn;

}

cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(0) green:(0) blue:(0) alpha:1];
cell.selectedBackgroundView = selectionColor;
cell.selectionStyle = UITableViewCellDragStateLifting;
return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;
button.selected = YES;

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {


UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;
button.selected = NO;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章