【问题标题】:UIImageView IBOutlet Properties for CollectionView Cells Not Loading未加载 CollectionView 单元格的 UIImageView IBOutlet 属性
【发布时间】:2015-01-24 20:50:16
【问题描述】:

我有一个 UITableView 和 UICollectionView 加载相同的内容,但是,即使它们的协议方法定义几乎相同,集合视图给我一个空视图,而表格视图显示内容。为了让您了解这个问题的上下文,我将介绍在两个视图上加载内容之前发生的事情:

  1. 我从 -imageWithData 取回图像并将其存储在字典中
  2. 这个字典随后被 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


【解决方案1】:

在 uicollectionView cellforrowatindexpath 的委托中

之后 CellViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

添加这个 如果(细胞 == 零) 单元格 = [[CellViewController alloc]init];

【讨论】:

  • 我试过了,但没有任何改变;视图仍然是空的。这可能与我添加单元格类的方式有关。它适用于 tableViewCell 图像属性,但不适用于我的 collectionView,它具有在 CellViewController 类中创建的自定义 IBOutlet imageView 属性。
  • 我还尝试添加一个 UILabel 类型的 IBOutlet,以查看问题是否出在图像本身或我如何引用 IBOutlet 属性“imageOfCell”。我没有运气,所以我猜我如何引用单元格的 UIImageView 插座存在问题
  • 问题可能是您从字典访问图像的方式,不要使用 initWithCIImage 方法,以与表视图委托相同的方式进行操作。 CIImage 是核心图像,我认为这不是您最初保存在该字典中的内容。
  • 这就是我正在做的。就像我说的,我尝试过使用 UILabel 但它没有用,所以问题可能在于我如何引用 IBOutlet
  • 你知道我该如何解决这个问题,或者你知道问题是什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多