【发布时间】:2014-03-11 13:02:44
【问题描述】:
我无法解决下一个问题。
我想显示 20 个表格单元格,每个单元格都包含一个唯一的UICollectionView。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *package=[_packageList objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"package";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UICollectionView *cellImageCollection=(UICollectionView *)[cell viewWithTag:9];
cellImageCollection.restorationIdentifier=[NSString stringWithFormat:@"%li", (long)indexPath.row, nil];
cellTracking.text=[NSString stringWithFormat:@"Row #%li",(long)indexPath.row];
return cell;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
int row=[collectionView.restorationIdentifier intValue];
return [[[_packages objectAtIndex:row] valueForKey:@"imageGallery"] count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
int row=[collectionView.restorationIdentifier intValue];
NSString *imgStrLink=[[[_packages objectAtIndex:row] valueForKey:@"imageGallery"] objectAtIndex:indexPath.row];
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageID" forIndexPath:indexPath];
UIImageView *imageView=(UIImageView *)[cell viewWithTag:2];
imageView.image=....;
return cell;
}
函数tableView:cellForRowAtIndexPath:被调用了20次,但collectionView:numberOfItemsInSection:和collectionView:cellForItemAtIndexPath:只被调用了4次。
如屏幕截图所示,UICollectionView 每四行重复一次。
我的错是什么?
【问题讨论】:
-
使用自定义 tableviewcell,为其创建一个子类,将您的 collectionview 委托/数据源添加到单元格的子类中。我自己并没有这样测试它,但是我使用了一个包含另一个集合视图的集合视图单元格并且它起作用了。
-
不要使用单元格作为数据源和委托。 Cell不应该负责提供这样的东西。最好将集合视图子类化并添加名为 index 的属性,然后在委托和数据源方法中检查索引。将您的视图控制器用于集合视图委托和数据源。
标签: ios uitableview uicollectionview