我已经使用github附加了我的示例项目,并提供了下面的链接
我有一个表视图,在该tableview中,单元格由一个UICollectionView组成。这意味着每个单元格都可以水平滚动到一定限度,比方说5。
tableView单元格和collectionview单元格都有自定义单元格类别
tableview单元格也有一个UIPageControl将在我们将水平滚动UICollectionViewCells时更改
我设法完成了这部分工作,但是假设我已经将第二个tableview单元的集合视图滚动到第三位,这在第9个tableview单元格上重复了一遍。
第9个tableview单元也有相同的数据。但是我还没有在9日设置任何东西。
所以这意味着问题在于滚动时可重用性和数据不正确。
我的代码到目前为止
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
实施类
//ViewController.m
#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "MyTableViewCell.h"
@interface ViewController ()
{
MyTableViewCell* tableViewCell;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark TableView Delegates
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{
return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
tableViewCell=(MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell"];
if(tableViewCell==nil)
{
tableViewCell=[[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyTableViewCell"];
}
UICollectionViewFlowLayout *flowLayout =
[[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[tableViewCell.collectionView setPagingEnabled:YES];
[tableViewCell.collectionView setCollectionViewLayout:flowLayout];
[tableViewCell.collectionView setBackgroundColor:[UIColor whiteColor]];
tableViewCell.pageControl.tag=indexPath.row;
tableViewCell.collectionView.tag=indexPath.row;
NSLog(@"Index path row %ld",indexPath.row);
[tableViewCell setSelectionStyle:UITableViewCellSelectionStyleNone];
tableViewCell.path=indexPath;
return tableViewCell;
}
#pragma mark CollectionView Delegates
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 5;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell* cell=(MyCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath];
if(cell==nil)
{
cell=[[MyCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width, collectionView.frame.size.height)];
}
[cell.myLabel setText:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return collectionView.frame.size;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if ([scrollView isKindOfClass:[UICollectionView class]])
{
UICollectionView *mainCollection = (UICollectionView *) scrollView;
NSIndexPath *myIP = [NSIndexPath indexPathForRow:mainCollection.tag inSection:0];
MyTableViewCell *cell = (MyTableViewCell *)[self.tableView cellForRowAtIndexPath:myIP];
CGRect visibleRect = (CGRect){.origin = mainCollection.contentOffset, .size = mainCollection.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [mainCollection indexPathForItemAtPoint:visiblePoint];
NSLog(@"visibleIndexPath %ld",(long)visibleIndexPath.row);
[cell.pageControl setCurrentPage:visibleIndexPath.row];
}
}
@end
单元格类
//MyTableViewCell.h
#import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) NSIndexPath * path;
@end
MyCollectionViewCell.h
//MyCollectionViewCell.h
#import <UIKit/UIKit.h>
@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
视图就像
我担心的是,每当我进行任何更改时,都可以在表格视图上的单元格1上说出并滚动其中的集合以说出第二个索引,然后每当我滚动整个表格时重复这个
注意:我已将表格的单元格数保持为10,以查看重复的影响。
我的示例项目的Github链接是https://github.com/RajanMaheshwari/TableAndCollection
答案 0 :(得分:0)
首先:
[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath]
始终返回视图,因此您不需要:
if(cell==nil)
{
cell=[[MyCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width, collectionView.frame.size.height)];
}
因为它不会发生。
你的真正原因是你所有UITableView只有一个单元格...... 保持tableCell作为属性的想法来自何处?
MyTableViewCell* tableViewCell;
你需要分别为每个indexPath创建一个单元格实例,现在你有一个显示多次的实例,这就是为什么更改其中一个的集合视图页面会影响其他行 - 因为它是同一个对象(视图)
删除该问题后,您需要记住元素将被重用 - 这意味着您的第一行实例,在第5行再次重复使用,在它从屏幕消失之后,所以在您将元素出列后,您需要将所有变量设置为此行。 你不能把你的变量保存在这一行中,因为它会被重用,所以即使你有100行,你将会在整个表中重复使用5-6个实例(取决于行高)。
您需要保留重要的变量,这些变量是在视图外部渲染每一行所必需的,并根据这些变量配置视图。
在你的情况下,你应该为每一行保留pageControl的页面索引 - 使用NSDictionary,并为你的UITableView中的每个indexPath保留一个NSNumber。 每当页面发生变化时,请更新此NSDictionary中的值(默认为零)。 当您使用集合视图创建单元格时,请根据您在NSDictionary中为此indexPath设置的内容为pageControl设置正确的值