簡體   English   中英

在iOS的UITableviewcell中選擇單選按鈕

[英]selecting radio button in UITableviewcell in ios

我在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 {

    }

}

並在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