删除UITableViewCell选择

Apo*_*llo 15 objective-c uitableview ios

目前我通过使用UITableViewSelectionStyleNone然后根据委托方法更改单元格的颜色来覆盖标准的UITableViewSelectionStyle :

- (void)tableView:(UITableView *)tableView 
      didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];                
    [cell setBackgroundColor:[UIColor yellowColor]];
}

- (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor whiteColor]];
}

- (void)tableView:(UITableView *)tableView 
    didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"indexpath: %i",indexPath.row);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor whiteColor]];
}

- (void)tableView:(UITableView *)tableView 
    didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor whiteColor]];
}
Run Code Online (Sandbox Code Playgroud)

这几乎是有效的,除了每当我突出显示一个单元格,然后将其手指从它上面拖出而没有实际选择它时,颜色不会变为白色...如果我将其设置为[UIColor RedColor]它的工作完全正常.为什么是这样...

编辑:

不知何故,当我在didUnhlightRowAtIndexPath之后打印出indexPath.row时,我从我的NSLog获得"indexpath:2147483647"

Ada*_*hns 32

你可以尝试:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

如果您只是希望在选择单元格后突出显示消失.除非我误解你的问题.


Hem*_*ore 19

你也可以试试这个

tableView.allowsSelection = NO;
Run Code Online (Sandbox Code Playgroud)

这样做的另一种方式

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Run Code Online (Sandbox Code Playgroud)

多一个

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Run Code Online (Sandbox Code Playgroud)


小智 6

您也可以从情节提要中执行此操作。选择 tableViewCell 并在 Attributes Inspector 下选择 Selection > None

在此处输入图片说明