2015-04-20 39 views
4

Issue:我只想显示带有复选标记的选定单元格。我不想要灰色的亮点。 我试过了:UITableView:选中具有复选标记但没有高亮的行

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]; 
} 
+0

你试过了什么?你在哪里使用了selectionStyle? – NSPunk

+3

'selectionStyle'是合适的属性。如果它不起作用,你把它放在错误的地方。向我们显示您输入的方法的代码。 – Dima

+0

尝试在单元格分配后添加行'cell.selectionStyle = UITableViewCellSelectionStyleNone;'if'块内部。 – itsji10dra

回答

1

如何:

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

selectionStyle属性应该足以除去亮点。

如果你想显示勾选使用已选择的行后,你需要实现tableView:didSelectRowAtIndexPath:方法

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

Swift,最简单的办法就是deselect a cell without animation它已被选中之后:

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

这更像是一种创造目标的创可贴方式。我建议在'willDisplayCell'中将'cell.selectionStyle'设置为'.None',这样选择就不会像Koen建议的那样出现在第一位。 – jeffjv

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


    } 
+0

谢谢你的回答尼沙尔。它没有工作。在选择它仍然有灰色背景。 – user3453784

+0

我修改了我的代码,你可以复制和粘贴整个代码一次 –

+0

仍然没有运气。 :( – user3453784

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

    } 
} 

加方本在我见过的其他解决方案是,这是一个相当小。无需跟踪实例变量或其他任何东西。