Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ios中的UITableviewcell中选择单选按钮_Ios_Xcode_Uitableview_Radio Button - Fatal编程技术网

在ios中的UITableviewcell中选择单选按钮

在ios中的UITableviewcell中选择单选按钮,ios,xcode,uitableview,radio-button,Ios,Xcode,Uitableview,Radio Button,我正在UITableview中显示姓名列表,我正在尝试使用单选按钮选择其中任何一个姓名。我面临的问题是,我无法选择单元格的特定行,或者选择了所有行,或者取消选择了所有行 在单元格中显示单选按钮的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UIButton *report_but; int flag; static NSSt

我正在UITableview中显示姓名列表,我正在尝试使用单选按钮选择其中任何一个姓名。我面临的问题是,我无法选择单元格的特定行,或者选择了所有行,或者取消选择了所有行

在单元格中显示单选按钮的代码:

- (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];
}

当选择第1行时

我不能使用didselectrowatindexpath方法进行单行选择,因为选择行不应选择单选按钮

有人能帮我选择这个tableviewcell的唯一一行吗。

试试这个

- (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 {

    }

}
单选按钮中:

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

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

不要使用
flag
而是使用
indepath
(nsindepath对象)存储所选行,并在
cellforRow
方法中检查该
indepath
。您可以将索引路径行的标记赋予按钮并获取该索引路径的单元格,而不是处理点。这将是一种更合适的方法。@PoojaManiklalBohora但如果单元格被添加、删除或重新排序,将indexPath设置为标记将不起作用。-(void)单选按钮:(id)按钮{UIButton*senderbutton=(UIButton*)按钮;NSLog(@“button tag%ld”,(long)senderbutton.tag);}始终返回0,作为按钮标记。我做错什么了吗@Poojamaniklalbohora在CellForRowatineXpath中创建标记时是否设置了标记?@sathya make
NSIndexPath*selectedIndexPath
作为实例变量,以便您可以在
CellForRowatineXpath
中对其进行比较,以选择或取消选择radiobutton。