【问题标题】:RadioButton inside Tableview cell iOSTableview单元iOS中的RadioButton
【发布时间】:2016-08-10 10:03:22
【问题描述】:

我已按照 RadioButton 的 this 教程进行操作。我已将它与 tableview 集成。问题是当我选择表格行时,单选按钮没有被选中。只有当我点击 RadioButton 时它才会被选中。我希望在选择表格行时选择单选按钮。代码如下:

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

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    }
    RadioButton *rb1 = [[RadioButton alloc] initWithGroupId:@"Group" index:indexPath.row];
    rb1.frame = CGRectMake(20,13,22,22);
    [cell.contentView addSubview:rb1];
    cell.textLabel.text = @"ABC";
    return cell;
}

RadioButton with Tableview

【问题讨论】:

  • 把你的选择代码放在 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }

标签: ios radio-button tableview selection


【解决方案1】:

//在“cellforRowAtIndexpath”中为你的Btn设置标签

rb1.tag=101;

在 didSelectRowAtIndexPath ->

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *btn = (UIButton*)[cell viewWithTag:101]; 
 btn.backgroundColor=[UIColor redColor];
btn.Selected = YES;
 [btn handleButtonTap:self];

【讨论】:

  • 我试过了,但无法设置按钮 isSelected:YES。
  • 点击单选按钮时,发送者值为 而当我单击 tableview 单元格时,发件人值为 >
  • UIButton btn = (UIButton)[cell viewWithTag:101];有了这个你可以访问你的表的选定行 Button(rbi Button(Index)),
  • 我们使用了上述教程中的单选按钮。 didSelectRowAtIndexPath 中的代码是 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UIButton *btn = (UIButton *)[cell viewWithTag:101]; btn.backgroundColor=[UIColor redColor]; btn.selected = 是; NSLog(@"按钮:%@",btn); [self radioButtonSelectedAtIndex:indexPath.row inGroup:@"Group"]; RadioButton *rb = [[RadioButton alloc] init]; //[rb handleButtonTap:self]; }
  • 这是一个复杂的库,你可以使用简单的按钮来实现这个功能,检查更新的答案
【解决方案2】:

didSelectRowAtIndexPath上执行以下代码

目标 C

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIButton *btn = (UIButton *)[cell viewWithTag:101];
        [btn setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateSelected];
        btn.selected = YES;
        NSLog(@"Button : %@",btn);
    }



    - (UIImage *)imageWithColor:(UIColor *)color {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return image;
    }

【讨论】:

  • 我在 tableview 单元格的 contentView 中添加了单选按钮。它不是自定义单元格。那么如何访问 didSelectRowAtIndexPath 中的单选按钮。
  • 你应该为你的按钮分配标签(假设 10),现在你可以得到这样的按钮 "var yourbutton = cell.contentView.viewWithTag(10) as!UIButton" 一旦你得到你的按钮其余代码是一样的
  • 我试过了,但无法设置按钮 isSelected:YES。它正在将错误无法识别的选择器发送到实例
  • 我们使用了上述教程中的单选按钮。 didSelectRowAtIndexPath 中的代码是 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UIButton *btn = (UIButton *)[cell viewWithTag:101]; btn.backgroundColor=[UIColor redColor]; btn.selected = 是; NSLog(@"按钮:%@",btn); [self radioButtonSelectedAtIndex:indexPath.row inGroup:@"Group"]; RadioButton *rb = [[RadioButton alloc] init]; //[rb handleButtonTap:self]; }
  • 这也行不通。给出这个错误:[RadioButton setBackgroundImage:forState:]: unrecognized selector sent to instance
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 2016-11-09
  • 2014-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多