简体   繁体   中英

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. 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. 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. Make sure that the VC containing the collection view is the delegate of the contained table views. 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)...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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