【发布时间】:2015-01-24 20:50:16
【问题描述】:
我有一个 UITableView 和 UICollectionView 加载相同的内容,但是,即使它们的协议方法定义几乎相同,集合视图给我一个空视图,而表格视图显示内容。为了让您了解这个问题的上下文,我将介绍在两个视图上加载内容之前发生的事情:
- 我从 -imageWithData 取回图像并将其存储在字典中
-
这个字典随后被 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”)顶部的。正如我所说,集合视图给了我一个没有内容的空白视图。在调试中,我看到“photo”实例和“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.
}
我对使用集合视图相当陌生,所以如果我缺少一些相对较小的东西,我不会感到惊讶。提前致谢!
【问题讨论】:
-
两个问题:\na) 您是否使用
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {设置单元格数?\nb) 您是否尝试过[UIImage imageNamed:self.streamImages[indexPath.row]]而不是[[UIImage alloc]initWithCIImage: self.streamItems[indexPath.row][@"ThumbnailPhoto"]];? -
@GlennRay 是的,-numberOfItemsInSection:返回 [self.streamItems 计数])。我刚刚尝试使用 -imageNamed: 就像你说的那样并得到一个运行时错误:NSInvalidArgumentException,原因:[NSDictionaryM stringByDeletingPathExtension]: unrecognized selector sent to instance... ???
-
[UIImage imageNamed:self.streamImages[indexPath.row][@"ThumbnailPhoto"]]怎么样? -
是的,我以为您只是忘记在第一条评论中添加 [@"ThumbnailPhoto"],所以我在去测试时添加了它。关于我的实现,您还有什么奇怪的地方吗?
标签: ios objective-c xcode uiimageview uicollectionview