【问题标题】:Change UIButton colour and text on click in a UITableView在 UITableView 中单击时更改 UIButton 颜色和文本
【发布时间】:2016-01-13 09:24:16
【问题描述】:

点击每个单元格时,我必须将此“关注”按钮更改为“关注”,当我再次点击“关注”时,它应该返回“关注”。 怎么可能?

【问题讨论】:

  • 在 UItableView 的didSelectRowAtIndexPath 委托中,将单元格的按钮文本设置为button.setTitleColor(UIColor.redColor(), forState state: .Normal) ,将文本设置为setTitle("Title", forState state: .Normal)。并添加一个标志来保存单元格的状态。

标签: ios uitableview uibutton


【解决方案1】:

创建从单元到控制器的动作。现在您可以参考您的单元格按钮

-(void)followButtonActionFromTableViewCell:(UIButton *)sender
{  
 [self followUnfolloWithButton:sender]
}

-(void)followUnfolloWithButton:(UIButton *)sender{
//you may need to call web service and set button accordingly

   [sender setTitle:@"Follow" forState:UIControlStateNormal];
    [sender setBackgroundColor:[UIColor yellowColor]];//set your colour

}

如果你想重新加载表格视图,请使用。

  UIButton * button = (UIButton*)sender;
  CGRect frameRect ;
   NSIndexPath * indexPath ;
   frameRect=[button convertRect:button.bounds toView:self.tableView];
   indexPath= [self.tableView indexPathForRowAtPoint:frameRect.origin];

//Reload 

  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];

在 cellForRowAtIndexPath 方法中保存跟随/跟随放置检查条件的状态。和

通过 sender.tag 更新您的模型

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

//...your code
cell.yourFollowButton.tag=indexPath.row;

//check condition and set button text and colour 

    }

【讨论】:

  • 暂时它看起来像工作,但在向上/向下滚动时失败,因为单元格在 tableview 中被重用。我们必须将关注/关注状态存储在单独的模型中。您必须根据模型中存在的值配置单元格。
  • @Akhil Jiji。立即查看答案
  • @AkhilJiji.Check 您的 indexPath.row 与模型数组相比可能超出索引。放检查条件 indexPath.row
【解决方案2】:

问题是当您单击按钮并更改按钮的某些属性时,也许您重新加载 tableView 或重新加载单元格。状态没有缓存。

按钮的颜色和文字会观察用户模型的属性(例如:followStatus),当点击按钮时,你应该改变模型的followStatus并重新加载单元格。

谢谢。

【讨论】:

    【解决方案3】:
    - (IBAction)btnStartOnClick:(id)sender {
    
       UIButton *someButton = (UIButton*)sender;
       NSString *str=[someButton titleForState:UIControlStateNormal];
    
      if ([str isEqualToString:@"Follow"]) 
       {
        [btn setTitle:@"Following" forState:UIControlStateNormal];
       }
      else if ([str isEqualToString:@"Following"])
      {
       [btn setTitle:@"Follow" forState:UIControlStateNormal];
      }
    

    【讨论】:

      【解决方案4】:

      我猜这很简单。在您的 tableview didSelectRowAtIndexPath 委托方法中,您只需像这样设置按钮颜色和标题。

         [buttonName setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
         [buttonName setTitle:@"Following" forState:UIControlStateNormal];
      

      当您再次单击同一个单元格时,您必须检查按钮文本。您可以通过设置一个布尔值来做到这一点。即,当您选择特定单元格时,将布尔值设置为“是”并将按钮文本设置为“关注”,然后再次单击同一单元格,检查是否设置了布尔值(如果已设置),您可以将文本更改为“关注”。其他选项是您必须将选定的 indexpath 存储在一个可变数组中,并且在每个选择的单元格上尝试在数组中推送和弹出该对象。这是其他步骤

      首先在你的 .h 文件中创建一个 Mutablearray

      @property (strong,nonatomic) NSMutableArray *selectedIndexPaths;
      

      在你的 viewDidLoad 方法中分配它。

       self.selectedIndexPaths = [[NSMutableArray alloc]init];
      

      在你的 .m 文件中添加这个方法

      - (void)addOrRemoveSelectedIndexPath:(NSIndexPath *)indexPath
      {
        BOOL containsIndexPath = [self.selectedIndexPaths containsObject:indexPath];
      
        if (containsIndexPath) {
          [self.selectedIndexPaths removeObject:indexPath];
        }else{
          [self.selectedIndexPaths addObject:indexPath];
        }}
      

      现在在您的 didSelectRowAtIndexPath 委托方法中添加此方法以检查该数组是否包含您选择的索引路径。

      [self addOrRemoveSelectedIndexPath:indexPath]//call of the method;
       BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath];
      
       if(isSelected)
       {
        //change your button text and color.
       }else{//unset your button text and color..}
      

      通过这种方式,您可以轻松管理单元格的索引路径。不管有没有冲突..

      【讨论】:

        猜你喜欢
        • 2015-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多