简体   繁体   English

在iOS的UITableviewcell中选择单选按钮

[英]selecting radio button in UITableviewcell in ios

Am display list of names in UITableview and I'm trying to select any one of the names by using radio button. 我在UITableview中显示名称列表,我正尝试使用单选按钮选择任何名称。 The problem I face is I can't select the specific row of the cell, either all the rows are selected or deselected. 我面临的问题是我无法选择单元格的特定行,所有行都已选择或取消选择。

Code for displaying radio button in cell: 在单元格中显示单选按钮的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *report_but;
int flag;

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == Nil) 
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
 cell.textLabel.text = [user_name objectAtIndex:indexPath.row];

report_but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
report_but.frame = CGRectMake(260,18,20,20);
[report_but setTitle:@"" forState:UIControlStateNormal];

if (flag == 0) {
    [report_but setBackgroundImage:[UIImage imageNamed:@"radio_blank.png"] forState:UIControlStateNormal];
}
else
[report_but setBackgroundImage:[UIImage imageNamed:@"radio.png"] forState:UIControlStateNormal];

[report_but addTarget:self action:@selector(radio_button:)forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:report_but];
return cell;
}

- (void)radio_button:(id)button
{

NSLog(@"radio button clicked");
NSLog(@"button tag %@",button);
if (flag == 0) {
    flag = 1;
}
else
    flag = 0;

[table_list reloadData];

} }

While row 1 is selected 选择第1行时 在此处输入图片说明

And I can't use didselectrowatindexpath method for this single row selection, since selecting the row should not select the radio button. 而且我不能使用didselectrowatindexpath方法来进行单行选择,因为选择行不应选择单选按钮。

Can any one help me to select the only one row of this tableviewcell. 有谁能帮助我选择此tableviewcell的唯一一行。

Try this, 尝试这个,

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

    UIButton *report_but;
    if (cell == Nil) 
    {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
         UIButton *report_but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
         report_but.frame = CGRectMake(260,18,20,20);
         [report_but setTitle:@"" forState:UIControlStateNormal];
         report_but.tag = 8888;
         [cell.contentView addSubview:report_but];
    } 

    report_but = (UIButton *)[cell.contentView viewWithTag:8888];
    if (selectedIndexPath && selectedIndexPath.row == indexPath.row) {

    } else {

    }

}

and in radio_button: 并在radio_button:

- (void)radio_button:(id)button
{
     CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:self.tableView];    

     selectedIndexPath = [self.tableView indexPathForRowAtPoint:pointInTable];
     [table_list reloadData];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM