[英]Use uicollectionview inside uitableviewcell
我無法解決下一個問題。
我想顯示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
,每隔UICollectionView
重復一次UICollectionView
。
我的錯是什么?
我也遇到了這個問題,我遇到這個問題的原因是因為tableview和collectionView的數據源和委托都是同一個父viewcontroller。
當我將collectionView的數據源和委托更改為另一個View說ParentView時,它開始為我工作,我將collectionView添加到此ParentView,然后最終添加ParentView作為單元格的conentView。
我之前的回答指出了兩個很好的教程被有人說我應該在這里包含內容的基本部分(因為鏈接可能在將來變得無效)...但我不能這樣做,因為那些是由教程所有者以及整個代碼也是必不可少的:),所以現在我給出了源代碼的鏈接......
-anoop
從Xcode 7和iOS9開始,您可以使用UIStackView - 如果collectionview或tableview相互包含,則可以設計最佳解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.