4

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

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];
}
4

6 回答 6

1

这个怎么样:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
于 2015-04-20T17:02:26.660 回答
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];


    }
于 2015-04-20T17:15:33.257 回答
0

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

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView cellForRowAtIndexPath:visibleIndexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
于 2015-04-20T17:07:40.140 回答
0

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

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
于 2015-04-20T17:11:36.193 回答
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;
}
于 2016-12-28T02:28:53.153 回答
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];

    }
}

与我见过的其他解决方案相比,这个解决方案的优点是这个解决方案非常小。无需跟踪实例变量或其他任何东西。

于 2017-01-23T11:26:43.070 回答