Ios UITableView和UICollectionView在行中同时在不同行中滚动

Ios UITableView和UICollectionView在行中同时在不同行中滚动,ios,uitableview,uicollectionview,Ios,Uitableview,Uicollectionview,我们有一个UITableView,每一行都作为UICollectionView支持水平滚动,而不启用分页 我们已经登记了细胞以供再次使用 // Setup for VenueFilterTableViewCell NSString * cellIdentifier = @"VenueFilterTableViewCell"; VenueFilterTableViewCell *tbCell = [self.tableView dequeueReusableCellWith

我们有一个
UITableView
,每一行都作为
UICollectionView
支持
水平滚动
,而不启用
分页

我们已经登记了细胞以供再次使用

    // Setup for VenueFilterTableViewCell
    NSString * cellIdentifier = @"VenueFilterTableViewCell";
    VenueFilterTableViewCell *tbCell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (tbCell == nil) {
        tbCell = [[VenueFilterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                 reuseIdentifier:cellIdentifier];
        tbCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    // Method for inflate the UICollectionView in the row
    [tbCell setVenues:venues
              loading:NO
           atLocation:self.definedLocation
     forFilter:filter
         withDelegate:self];
    cell = tbCell;
当我水平滚动行时,
UITableViewCell
indexPath.row
0处的
UICollectionView
,同时滚动
indexPath.row
3(最初在屏幕外)。所以,如果在水平滚动之后,快速向下滚动,您可以看到第3行和第7行。。。等等,同时滚动

我在每个单元格中都有一个进度条,用于向用户提供距离水平滚动结束有多远的反馈,但由于这种重用行为,涉及的每一行(0、3和7)都会弄乱另一行的进度

有什么建议吗

更新

我在
UITableView
中添加了下一个事件,用于控制单元格何时离开屏幕,并强制内部的
UICollectionView
停止滚动。这稍微提高了性能,但最终问题再次发生

UITableViewController中的代码,用于检测行何时离开屏幕:

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView.indexPathsForVisibleRows indexOfObject:indexPath] == NSNotFound) {
        // This indeed is an indexPath no longer visible
        // Do something to this non-visible cell...
        if ([cell isKindOfClass:[VenueFilterTableViewCell class]]) {
            VenueFilterTableViewCell *tbCell = (VenueFilterTableViewCell *) cell;
            [tbCell stopScrolling];
        }
    }
}
使用UICollection视图在UITableViewCell中编码,在重新加载内容中,除了恢复contentOffset外,我们还需要重新启用self.collectionView.scrollEnabled=YES

- (void) stopScrolling {
    self.collectionView.scrollEnabled = NO;
    [self.collectionView setContentOffset:self.collectionView.contentOffset animated:NO];
}

似乎是一个典型的问题造成的细胞重用。如果确实有一个
UICollectionView
作为单元格的一部分,最好的方法是创建一个存储偏移量的
Dictionary

快速示例:

var-storedOffsets:[Int:CGFloat]=[:]

tableView:didendisplaying:forRowAt:
中存储集合视图的
contentOffset
的偏移量


然后在
tableView:willDisplay:forRowAt:
中,使用这些存储的偏移量设置单元格中
collectionView
contentOffset

我的第一个答案没有帮助。所以我修正了这个,抱歉用swift,你可以很容易地用objc转换

我用以下代码实现了willDisplayCell和endDispayCell

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = cell as? ScreenShotsCell else { return }
    self.configureCell(for: indexPath, to: cell)
    cell.collectionView.setContentOffset(cell.collectionView.contentOffset, animated: true)
    cell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
}

//--------------------------------------------------------------------------------

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = cell as? ScreenShotsCell else { return }
    storedOffsets[indexPath.row] = cell.collectionViewOffset
    cell.collectionView.setContentOffset(cell.collectionView.contentOffset, animated: true)
}
主要的核心逻辑是cell.collectionView.setContentOffset,因此当它不可见时,它将停止滚动,因此它将修复由于重用单元格而导致的tableview滚动的其他行的问题


希望这有帮助

我之前已经询问过一条评论,但是其他用户删除了该评论,这不是偏移问题,正如我在更新中解释的,我已经在模型中保留了偏移量。如果在水平滚动后决定向下滚动,则用户可以同时看到重复使用的行。无论如何谢谢你@jakubThank!!肯定是willDisplay方法成功了!!非常感谢@Prashant真的很感激!