我有一个UITableView和UICollectionView加载相同的内容,但是,即使他们的协议方法几乎完全相同,集合视图给我一个空视图,而表视图显示内容。为了让您了解这个问题的背景,我将介绍在两个视图上加载内容之前发生的事情:
然后TableViewController类以及CollectionViewController类访问此字典并实现如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cell...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"User ID" forIndexPath:indexPath];
//setting UI
NSString *milesFrom = self.streamItems[indexPath.row][@"MilesAway"];
cell.textLabel.text = [NSString stringWithFormat:@"%@, Distance Away: %@", self.streamItems[indexPath.row][@"UserName"], milesFrom];
UIImage *photo = [[UIImage alloc]init];
photo = self.streamItems[indexPath.row][@"ThumbnailPhoto"];
cell.imageView.image = photo;
return cell;
}
这部分就像我需要的那样工作,但集合视图不是:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"MyCell";
CellViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImage *imageReturned = [[UIImage alloc]initWithCIImage: self.streamItems[indexPath.row][@"ThumbnailPhoto"]];
cell.imageOfCell.image = imageReturned;
[cell.imageOfCell sizeToFit];
return cell;
}
这个CellViewController类具有IBOutlet' imageOfCell'属性。此属性引用我拖动在我的集合视图内的单元格顶部(" MyCell")的图像视图对象。正如我所说,集合视图给了我一个没有内容的空白视图。在调试中,我看到了“照片”和“照片”。实例和' ImageReturned'实例具有相同的确切值。出于这个原因,我不知道为什么集合视图不会加载图像,但表视图会加载。如果它与我加载集合视图的方式有关,我已经包含了我的-viewDidLoad方法定义:
@implementation StreamCollectionViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass: [CellViewController class]forCellWithReuseIdentifier:@"MyCell"];
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
[flow setScrollDirection:UICollectionViewScrollDirectionVertical];
[self.collectionView setCollectionViewLayout:flow];
//create object to deal with network requests
StreamController *networkRequester = [[StreamController alloc]init];
[networkRequester getFeedWithCompletion:^(NSMutableArray *items, NSError *error) {
if (!error) {
self.streamItems = items;
[self.collectionView reloadData];
}
else{
NSLog(@"Error getting streamItems: %@", error);
}
}];
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
// Register cell classes
// [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
// Do any additional setup after loading the view.
}
我对使用集合视图相当陌生,所以如果它缺少相对较小的东西,我不会感到惊讶。提前谢谢!
答案 0 :(得分:0)
在uicollectionView cellofrowatindexpath的委托中
在 CellViewController * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
添加此内容 if(cell == nil) cell = [[CellViewController alloc] init];