我有一个UITableView,里面有一个UICollectionView。我假设当我调用[UITableView reloadData]时,也会重新加载collectionView数据,但事实并非如此。
正在发生的事情是加载了tableview,并且collectionView加载了一个名为placesArray的数组中的信息。但是当我执行查询并且placesArray发生更改时,我在Tableview上调用reloadData,但不再调用collectionView委托方法。
我正在使用AFTableViewCell,这是我从教程here
中获取的这就是AFTableviewCell.h的样子:
@interface AFIndexedCollectionView : UICollectionView
@property (nonatomic, strong) NSIndexPath *indexPath;
@end
static NSString *CollectionViewCellIdentifier = @"CollectionViewCellIdentifier";
@interface AFTableViewCell : UITableViewCell
@property (nonatomic, strong) AFIndexedCollectionView *collectionView;
- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath;
@end
和AFTAbleViewCell.m
@implementation AFIndexedCollectionView
@end
@implementation AFTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(10, 10, 9, 10);
layout.itemSize = CGSizeMake(44, 44);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[AFIndexedCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
[self.contentView addSubview:self.collectionView];
[self.collectionView reloadData];
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.collectionView.frame = self.contentView.bounds;
[self.collectionView reloadData];
}
- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
{
self.collectionView.dataSource = dataSourceDelegate;
self.collectionView.delegate = dataSourceDelegate;
self.collectionView.indexPath = indexPath;
NSLog(@"sec = %ld, row = %li", (long)indexPath.section, (long)indexPath.row);
self.collectionView.tag = indexPath.section;
[self.collectionView reloadData];
self.collectionView.backgroundColor = [UIColor clearColor];
}
@end
TableView方法:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FeaturedCell";
AFTableViewCell *cell = (AFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell == YES)
{
cell = [[AFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(AFTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setCollectionViewDataSourceDelegate:self indexPath:indexPath];
cell.collectionView.tag = indexPath.section;
NSInteger index = cell.collectionView.tag;
CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue];
[cell.collectionView setContentOffset:CGPointMake(horizontalOffset, 0)];
cell.collectionView.tag = indexPath.section;
}
集合视图方法
-(NSInteger)collectionView:(AFIndexedCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return placesArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
//// Setting up the cell here
return cell;
我也尝试在tableView委托方法中调用[cell.collectionView reloadData]但没有成功。
我希望你能提供帮助,
谢谢 - Shayne