2017-06-18 137 views
0

我想使用自定義單元格創建動態單選按鈕。我試過並能夠使用故事板創建。但如何只允許一個單選按鈕選擇?動態單選按鈕uitableview自定義單元格ios

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

    MChangeServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"changeServiceCellidentifier"]; 

    cell.serviceLabelView.text [email protected]"test"; 



    cell.radioButton.tag=indexPath.row; 


    [cell.radioButton addTarget:self action:@selector(radioButtonColorChange:) forControlEvents:UIControlEventTouchUpInside]; 

     return cell; 
} 

- (void)radioButtonColorChange:(id)sender 
{ 
    UIButton *button=(UIButton *) sender; 
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:button.tag inSection:0]; 
    MChangeServiceTableViewCell *tappedCell = (MChangeServiceTableViewCell *)[self.listTableView cellForRowAtIndexPath:indexpath]; 
    if ([tappedCell.radioButton.imageView.image isEqual:[UIImage imageNamed:@"radio_button_unselected.png"]]) 
    { 
     [tappedCell.radioButton setImage:[UIImage imageNamed:@"radio_button_selected.png"] forState:UIControlStateNormal]; 
    } 
    else 
    { 
     [tappedCell.radioButton setImage:[UIImage imageNamed:@"radio_button_unselected.png"] forState:UIControlStateNormal]; 
    } 
} 

我能夠通過點擊它。但是如何改變剩餘的單選按鈕選中的狀態時,按下一個按鈕更改單選按鈕的形象?

+0

如果你有問題,你可以上傳你與什麼不工作解釋清楚試了一下,並提供[最小,完整,可驗證的示例]( https://stackoverflow.com/help/mcve)。我建議閱讀[如何問](http://stackoverflow.com/help/how-to-ask)一個很好的問題。此外,一定要採取[旅遊](https://stackoverflow.com/tour) –

回答

0

我建議你這樣保存在另一個變量當前選擇的按鈕。

@interface YourViewController() 
@property (strong, nonatomic) UIButton *currentBtn; 
@property (nonatomic) NSInteger currentSelectedIndex; 

@end 

然後

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    _currentSelectedIndex = -1; 
} 


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

    MChangeServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"changeServiceCellidentifier"]; 

    cell.serviceLabelView.text [email protected]"test"; 

    cell.radioButton.tag=indexPath.row; 

    [cell.radioButton setImage:[UIImage imageNamed:@"radio_button_selected.png"] forState:UIControlStateSelected]; 
    [cell.radioButton setImage:[UIImage imageNamed:@"radio_button_unselected.png"] forState:UIControlStateNormal]; 
    [cell.radioButton addTarget:self action:@selector(radioButtonColorChange:) forControlEvents:UIControlEventTouchUpInside]; 

    [cell.radioButton setSelected:NO]; 

    if (indexPath.row == _currentSelectedIndex) { 
     [cell.radioButton setSelected:YES]; 
     _currentBtn = cell.radioButton; 
    } 

    return cell; 
} 

- (void)radioButtonColorChange:(id)sender 
{ 
    _currentBtn.selected = NO; 
    UIButton *button=(UIButton *) sender; 
    button.selected = !button.isSelected; 
    _currentBtn = button; 
    _currentSelectedIndex = button.tag; 
} 
+0

,我使用上面的代碼,我可以選擇單選按鈕。但是當我滾動桌面時,我可以看到從底部的單元格單元格單選按鈕之一被選中狀態。 – sampath

+0

我編輯我的帖子,請嘗試該代碼。 – vp2698

+0

仍然是相同的結果。其他單元格獲取按鈕圖像選擇。 – sampath