UITableViewCellSelectionStyleNone不起作用

时间:2013-08-10 08:15:58

标签: iphone objective-c uitableview

我正在尝试更改所选的UITableViewCell,但这里发生了一些奇怪的事情。我放了两张图片。请看一下: enter image description here enter image description here

当我选择“早期”单元格时,它会变为蓝色。它转移到'早期的等等'UIViewController。然后,当我点击“查找”UIButton时,它会返回到第一个视图。 它工作,但细胞仍然选择蓝色!所以我写了一些代码如下:

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

    if ([_currentMethod isEqual: @"searchIt"]) {

        if (indexPath.row == 0) {
            BookMark2ViewController* bookMark2 = [[BookMark2ViewController alloc] init];


            UITableViewCell* cell = [self tableView:_tableView
                                            cellForRowAtIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;



            [self.navigationController pushViewController:bookMark2 animated:YES];

        }else if (indexPath.row == 1) {
            BookMarkViewController* bookMark1 = [[BookMarkViewController alloc] init];

            [self.navigationController pushViewController:bookMark1 animated:YES];

        }
    }

代码中间的两行:

        UITableViewCell* cell = [self tableView:_tableView
                                        cellForRowAtIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

这两行不起作用。有没有人知道如何解决这个问题?

2 个答案:

答案 0 :(得分:5)

如果您不希望单元格显示选择,请将cell.selectionStyle = UITableViewCellSelectionStyleNone放在cellForRowAtIndexPath:方法中。

如果您想要选择单元格然后取消选择,请放置

[tableView deselectRowAtIndexPath:indexPath animated:YES];

didSelectRowAtIndexPath:

希望这会有所帮助

答案 1 :(得分:1)

UITableViewCell* cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath];
// Make sure your cell is not nil.
cell.selectionStyle = UITableViewCellSelectionStyleNone;

我有这样的情况。

相关问题