简体   繁体   English

uicollectionviewcell内的uitableview滚动问题

[英]Scrolling issue with a uitableview inside a uicollectionviewcell

I have a uitableview inside a uicollectionviewcell, but I am having problems with the contentoffset when there are more than 2 items in the uicollectionview. 我在uicollectionviewcell中有一个uitableview,但是当uicollectionview中有两个以上项目时,contentoffset出现问题。 If I scroll the first item's tableview and I swipe to the third item, the third item's tableview is at the same contentoffset as the first one. 如果滚动第一项的表视图,然后滑动到第三项,则第三项的表视图的内容偏移量与第一项的表相同。 Different data, just the same contentoffset. 不同的数据,只是相同的contentoffset。 This happens with even and odd numbered items. 这发生在偶数和奇数编号的项目上。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Without looking at code, it's likely that you're seeing the same content offset in the tables because you're looking at the same tables, reused as the cells containing them are reused. 如果不查看代码,您可能会在表中看到相同的内容偏移量,因为您正在查看的是相同的表,这些表已被重用,因为包含它们的单元格已被重用。

If you'd like the table views to remain at their current content offsets, then the collection view datasource will need to remember the content offset of the table at each index path and update those offsets as it configures each cell. 如果您希望表视图保持其当前内容偏移量,那么收集视图数据源将需要记住每个索引路径中表的内容偏移量,并在配置每个单元格时更新这些偏移量。

eg in almost-code, if the model is an array, then create an array of content offsets: 例如,以几乎是代码的方式,如果模型是一个数组,则创建一个内容偏移量数组:

@property ... NSMutableArray *tableOffsets;

Initialize it with [NSValue valueWithCGPoint:CGPointZero] , one for each element in the model. 使用[NSValue valueWithCGPoint:CGPointZero]初始化模型中的每个元素。 Make sure that the VC containing the collection view is the delegate of the contained table views. 确保包含集合视图的VC是所包含表视图的委托。 When the table view scrolls, find out which cell contains it and write down the content offset. 当表格视图滚动时,找出包含它的单元格并记下内容偏移量。

- (void)scrollViewDidScroll:(UITableView *)tableView {
    CGPoint co = tableView.contentOffset;
    self.tableOffsets[indexPath.row] = [NSValue valueWithCGPoint:co];

To get that index path, see the accepted answer here . 要获取该索引路径, 请在此处查看已接受的答案 Then in cellForItemAtIndexPath, restore the contained table view's offset (which may have been changed when it was reused)... 然后在cellForItemAtIndexPath中,还原包含的表视图的偏移量(重用时可能已更改)...

UITableView *containedTableView = // however you get this
containedTableView.contentOffset = [self.tableOffsets[indexPath.row] CGPointValue];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM