UITableView:选中带有复选标记但没有突出显示的行

时间:2015-04-20 16:42:20

标签: ios uitableview checkmark

问题:我只想显示带有复选标记的选定单元格。我不想要灰色的亮点。 我试过了:

cell.selectionStyle = UITableViewCellSelectionStyleNone

但没有工作。

以下是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];                           
      }

     ProfileSelection *profile = [self.profileSelections objectAtIndex:indexPath.row];
     cell.textLabel.text = [profile profileName];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     return cell;
}

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

    [self.profileSelectionsTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    ProfileSelection *profile = [self.profileSelections objectAtIndex:indexPath.row];
    self.mobileProfileId = [profile.profileId stringValue];
    [_continueButton setEnabled:YES];
}

6 个答案:

答案 0 :(得分:1)

这个怎么样:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

答案 1 :(得分:0)

selectionStyle属性应足以删除突出显示。

如果要在使用选择行后显示复选标记,则需要实现tableView:didSelectRowAtIndexPath:方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView cellForRowAtIndexPath:visibleIndexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

答案 2 :(得分:0)

Swift中,最简单的方法是在deselect a cell without animation被选中后立即:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
}

答案 3 :(得分:0)

删除此功能

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

尝试使用此问题将解决您的问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone
    cell.textLabel.text = @"Checking Table View";
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [tableView deselectRowAtIndexPath:indexPath animated:YES];


    }

答案 4 :(得分:0)

转到故事板。选择表视图单元格。将选择设置为无。然后添加以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath
{
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

答案 5 :(得分:0)

这个帖子看起来有点旧,但实际上我最近遇到了一个不错的解决方案。万一有人偶然发现这个帖子,我想我也可以发一个答案。特别是因为我花了好几个小时试图追踪它。

我再也找不到我的原始资料了,但这是我发现的:

突出显示是UITableViewCell属性selectedBackgroundView上的背景视图。如果我们有表格单元格的子类,我们可以使用willTransitionToState:检测到编辑状态的转换,并将背景视图设置为清晰/白色而不是蓝色/灰色样式。

这是我最终做的事情,而且效果很好。

- (void)willTransitionToState:(UITableViewCellStateMask)state {
    [super willTransitionToState:state];

    // To remove the blue/gray highlight on the table cell when selected,
    // set the selection background to a clear view.

    if (state & UITableViewCellStateShowingEditControlMask) {

        // If we're moving to an edit state, set the custom background view.

        UIView *bgView = [[UIView alloc] init];
        [bgView setTintColor:[UIColor whiteColor]];
        [bgView setBackgroundColor:[UIColor clearColor]];
        [self setSelectedBackgroundView:bgView];

    } else {

        // Otherwise, just remove it completely.
        // The system should handle the rest.

        [self setSelectedBackgroundView:nil];

    }
}

对于我所看到的其他解决方案,这方面的优势在于这一点非常小。无需跟踪实例变量或其他任何内容。