iOS高亮多个UITableViewCells

标签 ios objective-c uitableview

是否可以突出显示多个 UITableViewCell?像过滤表之类的东西。如果用户点击第 0 行,该行就会突出显示。然后用户点击第 1 行,该行也会突出显示,但第 0 行也保持突出显示。我还没有看到任何这方面的例子。

我有一个字典,其中包含 5 个可以选择的项目,其值为 yes 或 no 来跟踪哪些项目被突出显示。

我使用了 (UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath 并将其设置为 nil 但现在我无法取消选择行。请问有什么帮助吗?

最佳答案

要实现这一点,您可以做的是跟踪所选索引并相应地更改表 viewCell 的背景颜色,例如:

将数组声明为 SelectedIndexes

然后在您的 didSelectRowAtIndex 方法中执行此操作:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 if([selectedIndex containObject :indexPath]){

       [selectedIndexes removeObject:indexPath];

    }else{

      [selectedIndexes addObject:indexPath];


     }

  [tableView reloadData];

然后在您的 CellForRowAtIndexPath 方法中执行此操作

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

    if([selectedIndex containObject :indexPath]){

    cell.backgroundColor=highlightedColor; }其他{

    cell.backgrounfColor=NormalColor;

    }
    

} 希望这有帮助

关于iOS高亮多个UITableViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24147727/

相关文章:

ios - 如何在信号链中发送错误

ios - pubnub-chat objective-c 的用户列表

ios - 名为 "image"的属性没有可见的 ivar 吗?

iOS 7 Dev - @synthesize 不自动创建 ivars

swift - 如何在 UITabBarController 中的另一个 UINavigationController 上显示 UINavigationController?

Swift - 使用 .addTarget 为 UITableViewCell 制作一个按钮

ios - 在 UITableView 中插入一行后调用 numberOfSectionsInTableView

ios - Objective-C 桥接问题。如何在 Swift 项目中使用 Obj-C 框架?

ios - 如何在 View Controller 的背景中设置自定义颜色

ios - iTunes 连接 : Can I upload a new build with an old version?