【问题标题】:Get the RadioButton titlelabel.text value in UITableView Objective C获取 UITableView Objective C 中的 RadioButton titlelabel.text 值
【发布时间】:2018-08-05 14:05:32
【问题描述】:

在 UITableView 中加载动态数据。我有 2 个 RadioButtons(groupButtons)btnPaulbtnAdam 标题 Paul 和 Adam 在 UITableView 和一个 label(lblCheckStatus)。如果[cell.btnPaul setSelected:YES]; btnPaul.titlelebel.text 应该分配给标签,即保罗,如果[cell.btnPaul setSelected:NO]; 标签应该在UITableView 中为空。我已经尝试过以下代码,但无法获得正确的响应以获取空值。 TIA

// cellForRowAtIndexPath:

NSMutableDictionary* respDict = [respnseArray objectAtIndex:indexPath.row];

NSString *str = [respDict objectForKey:@"Name"];

if ([str isEqualToString:@"Paul"]) {
    [cell.btnPaul setSelected:YES];
} else {
    [cell.btnAdam setSelected:YES];
}

cell.btnPaul.tag = +indexPath.row;
[cell.btnPaul addTarget:self action:@selector(btnCheckClicked:) forControlEvents:UIControlEventTouchUpInside];

cell.btnPaul.tag = +indexPath.row;
[cell.btnPaul addTarget:self action:@selector(btnCheckClicked:) forControlEvents:UIControlEventTouchUpInside];

// RadioButton Checked
- (IBAction)btnCheckClicked:(RadioButton *)sender {

    UIButton *btn = (UIButton *)sender;
    NSUInteger tag = btn.tag;

    static NSString *CellIdentifier = @"Cell";

    FriendsCell *cell = (FriendsCell *) [_btlFriends dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.lblCheckStatus.text = sender.titleLabel.text;

    NSLog(@"%@",cell.lblCheckStatus.text);
}

【问题讨论】:

  • 为什么添加= +indexPath.row;

标签: ios objective-c uitableview xcode9


【解决方案1】:

首先,在设置单选按钮标签时删除+

cell.btnPaul.tag = indexPath.row;
[cell.btnPaul addTarget:self action:@selector(btnCheckClicked:) forControlEvents:UIControlEventTouchUpInside];

然后使用标签获取indexPath。当您使用 indexPath 时,您可以访问单元格以及单元格组件。希望这会对你有所帮助。

- (IBAction)btnCheckClicked:(RadioButton *)sender {

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
    FriendsCell *cell = [_btlFriends cellForRowAtIndexPath:indexPath];

    cell.lblCheckStatus.text = cell.btnPaul.titleLabel.text;

    NSLog(@"%@",cell.lblCheckStatus.text);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2017-02-23
    • 2017-06-14
    相关资源
    最近更新 更多