ios - 在 UITableView 中单击时更改 UIButton 颜色和文本

标签 ios uitableview uibutton

enter image description here

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

最佳答案

创建从单元格到 Controller 的 Action 。现在您可以引用您的单元格按钮

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

}

如果你想重新加载 TableView ,请使用。

  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 方法中保存 follow/following put 检查条件的状态。和

update your model by sender.tag

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

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

//check condition and set button text and colour 

    }

关于ios - 在 UITableView 中单击时更改 UIButton 颜色和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34762750/

相关文章:

ios - 在我快速单击另一个按钮之前,按钮图像不会在单击时发生变化

ios - iOS-UIButton文本左对齐等于另一个UIButton包含图像的文本

ios - 在 xcode5 中找不到适用于 ios 5 的 Storyboard

ios - 如何从 native iOS 应用程序启动 Unity 3D 应用程序

iOS Swift - 加载自定义表格单元格

非分组表的 UITableView 部分标题高度

ios - 如何修改 iPhone plist 以添加另一个级别并在代码中访问数据

ios - UITableView - 选择时出现问题

ios - Xcode - 为运行和测试操作设置不同的类

ios - 我需要将一些文件从现有的git repo移到新文件。我可以保留他们的历史吗?