UITableView 选定的单元格在滚动时不会保持选中状态

问题描述 投票:0回答:12

滚动表格时表格视图单元格无法保持其“选定”状态时遇到问题。这是相关代码:

@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedIndexPath = indexPath;
    //do other stuff
}

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

    MyCustomCell_iPhone* cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCell_iPhone"];

    if (cell == nil)
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell_iPhone" owner:self options:nil] objectAtIndex:0];

    if ([indexPath compare: self.selectedIndexPath] == NSOrderedSame) {
        [cell setSelected:YES animated:NO];
    }

    return cell;
}

对于单元格:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    if (selected) {
        self.selectedBg.hidden = NO;
    }else{
        self.selectedBg.hidden = YES;
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];

    if (highlighted) {
        self.selectedBg.hidden = NO;
    }else{
        self.selectedBg.hidden = YES;
    }
}

如何让选定的单元格保持突出显示?如果我将它滚动到屏幕外,当它在屏幕上滚动回来时,它会显示为未选中状态(其 selectedBg 已隐藏)。

编辑: 从单元格中删除 setHighlighted 方法可解决此问题。但是,这意味着我在按下表格单元格时没有突出显示状态。我想知道解决这个问题的方法。

ios uitableview
12个回答
14
投票

有同样的问题,选中的单元格的 accessoryView 在滚动时消失了。我的同事发现了这个问题的绝招。原因是在 iOS 7 中 touchesBegan 事件 UITableView 取消选择选定的单元格并选择已触及的单元格。在 iOS 6 中它不会发生并且在滚动时选定的单元格保持选中状态。要在 iOS 7 中获得相同的行为,请尝试:

  1. 在 tableView 中启用多项选择。

  2. 转到 tableView 委托方法 didSelectRowAtIndexPath,并取消选择使用代码触及的单元格:

NSArray *selectedRows = [tableView indexPathsForSelectedRows];
for(NSIndexPath *i in selectedRows){
    if(![i isEqual:indexPath]) {
        [tableView deselectRowAtIndexPath:i animated:NO];
    }
}

7
投票

我知道我的方法不是很正统,但似乎管用。这是我的解决方案:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if cell.selected {
        cell.selected = true
    } else {
        cell.selected = false
    }
}

您还必须实施您在帖子中提到的所有方法 (@soleil)


4
投票

我正在使用 Xcode 9.0.1 和 Swift 4.0。当单元格离开屏幕并返回时,我发现以下代码解决了我的选择标记:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.isSelected {
        cell.accessoryType = .checkmark
    } else {
        cell.accessoryType = .none
    }
}

2
投票

iOS 7/8 都在滚动开始时取消选择单元格(正如 Alexander Larionov 指出的那样)。

对我来说一个更简单的解决方案是在我的 ViewController 中实现这个 UIScrollViewDelegate 方法:

 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
     NSInteger    theRow       = [self currentRowIndex]; // my own method
     NSIndexPath *theIndexPath = [NSIndexPath indexPathForRow:theRow inSection:0];
     [self.myTableView selectRowAtIndexPath:theIndexPath 
                                   animated:NO 
                             scrollPosition:UITableViewScrollPositionNone];
 }

这是可行的,因为我的 viewController 是 UITableView 的委托,而 UITableView 继承自 UIScrollView。


1
投票

如果你想在 Swift 中实现同样的事情,那么这里是代码。顺便说一下,我正在使用 Xcode 7.2 和 Swift 2.1.

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if cell.selected == true{
        cell.selected = true
        cell.backgroundColor = UIColor.blackColor()
    }else{
        cell.backgroundColor = tableViewCellColor //Don't panic its my own custom color created for the table cells.
        cell.selected = false
    }
}

做任何你想要的其他定制..

谢谢..

希望这有帮助。


1
投票

Swift 3 解决方案,2017.

我用这行简单的代码解决了这个问题:

cell.isSelected = tableView.indexPathsForSelectedRows?.contains(indexPath) ?? false

tableView(tableView:cellForRowAt indexPath:)方法里面:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // Dequeue a reusable cell
    if let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellID") {

        cell.isSelected = tableView.indexPathsForSelectedRows?.contains(indexPath) ?? false

        // Now you can safely use cell.isSelected to configure the cell 

        // ...your configurations here

        return cell
    }

    return UITableViewCell()
}

1
投票

斯威夫特 5

将以下代码放入您的自定义

UITableViewCell
子类中:

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    guard !isSelected else { return }

    super.setHighlighted(highlighted, animated: animated)
    if highlighted {
        // Style cell for highlighted
    } else {
        // Style cell for unhighlighted
    }
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    if selected {
        // Style cell for selected
    } else {
        // Style cell for unselected
    }
}

解释: 尝试在

setHighlighted
setSelected
上设置断点。您会发现
dequeueReusableCell
方法依次调用
setSelected
setHighlighted
以重置新单元格。因此,您的突出显示代码会破坏您在选择代码中所做的样式。非黑客修复是为了避免在调用
setHighlighted(false, animated: false)
时破坏您选择的样式。


0
投票

您是否尝试过比较索引路径的行而不是整个索引路径对象?

if ((indexPath.row == self.selectedIndexPath.row) && (indexPath.section == self.selectedIndexPath.section)) {
    [cell setSelected:YES animated:NO];
}

0
投票

这是我想出的解决方案——它甚至不会让人觉得很老套。

1)实施

-scrollViewWillBeginDragging:
-scrollViewWillEndDragging:withVelocity:targetContentOffset:
并在滚动过程中手动突出显示所选行(如果有的话)的单元格。

我的样子是这样的:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollview {
    self.scrollViewIsDragging = YES;

    if( [self.tableView indexPathForSelectedRow] ) {
        [[self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]] setHighlighted:YES];
    }
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    self.scrollViewIsDragging = NO;

    if( [self.tableView indexPathForSelectedRow] ) {
        [[self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]] setHighlighted:NO];
    }
}

scrollViewIsDragging
属性在那里,因此在
-tableView:cellForRowAtIndexPath:
中我们可以确保任何新出列的单元格都有适当的突出显示(例如,如果所选行的单元格在离开屏幕后滚动到屏幕上)。该方法的相关部分如下所示:

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

    // ... cell creation/configuration ...

    if( self.scrollViewIsDragging && [[tableView indexPathForSelectedRow] isEqual:indexPath]) {
        [cell setHighlighted:YES animated:NO];
    }

}

……就在这里。 selectedRow 的单元格将在滚动期间保持突出显示。


0
投票

UITableViewCell 有一个 BOOL 属性“selected”。每当您加载单元格时,检查选定状态并相应地进行选择或取消选择,如下所示在 cellForRowAtIndexPath 定义中:

if (cell.selected) {
    // Maintain selected state
}
else{
     // Maintain deselected state
}

0
投票

在这里发布了一个快速答案: https://stackoverflow.com/a/35605984/3754003

在里面,我也解释了为什么会这样。


0
投票

不要使用内置系统属性

isSelected
.

您可以创建自己的财产,例如:

var isSelectedStyle = false
cell.isSelectedStyle = ....
© www.soinside.com 2019 - 2024. All rights reserved.