【问题标题】:Change images in dynamic radio buttons更改动态单选按钮中的图像
【发布时间】:2016-05-12 13:31:47
【问题描述】:

在表格视图单元格中我正在动态创建单选按钮。如果我单击一个按钮,那么我想将其余按钮更改为禁用

 -(void)RadioButton:(UIButton *)Sender
 {
     int i=Sender.tag;
 }

在此方法中,我只能更改单击的按钮图像。

【问题讨论】:

  • 如何从一个按钮更改另一个按钮的图像
  • @SanketBhavsar 你做过动态用户界面吗?
  • 单选按钮出现在所有单元格中?
  • stackoverflow.com/a/32862434/3883040 这可能对你有帮助..
  • @user6April 它根本不适合我。

标签: ios objective-c iphone swift ios7


【解决方案1】:

我会使用 UITableView 重新加载 API 来解决这个问题(reloadRowsAtIndexPaths:withRowAnimation:reloadSections:withRowAnimation:)。只需在 cellForRowAtIndexPath: 中设置所有按钮的启用状态,并在重新加载表格的某些部分之前修改有关这些状态的模型信息。尽量避免reloadData

例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.button.enable = yourModel.canDoSomething;
...
}


-(void)radioButtonDidChange:(UIButton *)Sender
{
    yourModel.canDoSomething = NO;
    [self.tableView reloadRowsAtIndexPaths:<indexPaths> withRowAnimation:UITableViewRowAnimationAutomatic];
}

【讨论】:

    【解决方案2】:

    假设您想更新所有单元格中的单选按钮,一旦点击按钮以在所有单元格中反映这一点,您就必须重新加载表格视图。你可以试试下面的代码

     NSInteger selectedButtonIndex = -1;
     -(void)RadioButton:(UIButton *)Sender
     {
         selectedButtonIndex=Sender.superview.tag;
         // reload table view
     }
    
     -(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //create your table view cell..
    
        if selectedButtonIndex == -1 {
             //set button state for no button selected
        }
        else if selectedButtonIndex == indexPath.row {
             //enable button
        }
        else {
            //disable button
        }
    }
    

    【讨论】:

    • 对不起,它对我没有帮助。一旦点击表格,我就无法重新加载表格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2012-11-07
    • 2011-03-29
    • 1970-01-01
    • 2021-11-25
    • 2012-04-06
    相关资源
    最近更新 更多