简体   繁体   English

UITableView多项选择

[英]UITableView multiple selection

I have a UITableView that I want to use to allow multiple selections. 我有一个UITableView我想用来允许多个选择。 I have rolled together most of the functionality, but am having one small problem. 我将大多数功能放在一起,但是有一个小问题。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Selected");
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
        [selectedCell setBackgroundColor:[UIColor grayColor]];
        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [[selectedCell textLabel] setTextColor:[UIColor whiteColor]];
    } else {
        [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
        [selectedCell setBackgroundColor:[UIColor clearColor]];
        [[selectedCell textLabel] setTextColor:[UIColor blackColor]];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

This is the code I am using to keep the cell selected, however when the user taps on the cell, the aaccessory view turns white and then turns back to blue. 这是我用来保持选定单元格的代码,但是当用户点击该单元格时,辅助视图会变成白色,然后又变回蓝色。

What is the best way for me to keep the accessory view white? 对我来说,使附件视图保持白色的最佳方法是什么?

Thanks 谢谢

I believe easy way to achieve this is by creating custom imageview with checkmark image(you need to get it as white checkmark) and set it as accessory view when selected. 我相信实现此目的的简单方法是通过创建带有选中标记图像的自定义imageview(您需要将其设为白色选中标记)并将其设置为选中的附件视图。

This will avoid the standard behavior of the accessoryview and achieve your goal. 这将避免附件视图的标准行为,并实现您的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM