• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - PF ImageView 不适用于 PF CollectionViewCell 的子类

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:37:54 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我很难获得 PFCollectionViewCell 的子类和 PFImageView 玩得很好. 我正在使用 Storyboards 来装配我的 PA_ProfileCollectionViewCell。

我想要做的就是加载一个图像(我可以从解析中成功获得),并将其分配给 PFImageView在我的自定义单元格中。我会很感激任何可以帮助我用棒球棒砸我的电脑,并在慢动作的荣耀中摧毁我的办公室的任何帮助/见解。

我想要的结果:PFCollectionViewCell 使用自定义单元格( -collectionView:cellForItemAtIndexPathbject: 的子类)

问题:我的自定义属性 PFImageView *imageThumb (在我的子类 PFCollectionViewCell 上)不响应 loadInBackground:setFile:这是 PFImageView 上的两种方法类(class)。

为了清楚起见,这是我的代码:

- 我的自定义 PFCollectionViewCell:ProfileCollectionViewCell

@interface PA_ProfileCollectionViewCell : PFCollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageThumb;
@end

- 在 vi​​ewDidLoad 中注册自定义类
- (void)loadView {
    [super loadView];
    [self.collectionView registerClass:[PA_ProfileCollectionViewCell class]
            forCellWithReuseIdentifier:_cellIdentifier];
}

- 在 collectionView:cellForItemAtIndexPathbject 中设置单元格
这是主要问题发生的地方。根据 cell.backgroundColor:,我的单元格值已成功绘制为深灰色调用,但所有将图像分配给我的 imageThumb 的尝试都失败了。请注意,在下面的源代码中,我的 imageThumb不响应选择器 loadInBackgroundsetFile , 附带 PFImageView .
-(UICollectionViewCell *)collectionViewUICollectionView *)collectionView
                  cellForItemAtIndexPathNSIndexPath *)indexPath
                                  objectPFObject *)object {


    PA_ProfileCollectionViewCell *cell = (PA_ProfileCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:_cellIdentifier
                                                                                                                   forIndexPath:indexPath];
    cell.backgroundColor = [UIColor grayColor];

    // Load the photo
    PFFile *pingThumb = [object objectForKey"imageMediumFile"];
    if (pingThumb){

        BOOL canLoadInBackground = [cell.imageThumb respondsToSelectorselector(loadInBackground];
        BOOL canSetFile = [cell.imageThumb respondsToSelectorselector(setFile];

        NSLog(@"can cell.imageThumb loadInBackground? : %hhd", canLoadInBackground);
        NSLog(@"can cell.imageThumb setFile? : %hhd", canSetFile);

        [cell.imageThumb setFile:pingThumb];
        [cell.imageThumb loadInBackground:^(UIImage *image, NSError *error) {
            if(!error){
                NSLog(@"CODE WON'T REACH THIS POINT.");
                NSLog(@"loadInBackground doesn't exist on cell.imageThumb)");
                [UIView animateWithDuration:0.2f animations:^{
                    cell.imageThumb.alpha = 1.0f;
                }];
            }
        }];

    }

    return cell;
}

输出所有NSLog()上面代码片段中的语句是:
can cell.imageThumb loadInBackground? : 0 (NO)
can cell.imageThumb setFile? : 0 (NO)

我尝试过的其他事情:

我已经选择了 ParseUIDemo 上的源代码,但他们所有的实现都使用股票 PFCollectionViewCell没有任何自定义或子类化,所以我尝试调整我的项目以使用他们的方法,但我也永远无法让事情在那里工作。

我已通读 this answer , 在 SO 上,这让我尝试爬过我的 cell.contentViews.subviewscell.subviews寻找PFImageView两次尝试都没有成功:
// [DIDN'T WORK]
for (UIView *subview in cell.contentView.subviews) {
    if ([subview isKindOfClass:[PFImageView class]]) {
        NSLog(@"Yay! I found a PFImageView"); // Never gets called
        break;
    }
}

然后我尝试了 this answer在 SO 上,这让我尝试获取 PFImageView 的实例从我的 Storyboard使用标签,这同样不成功。
PFImageView *photoView = (PFImageView *)[cell.contentView viewWithTag:242];
if(!photoView) NSLog(@"Can't find PFImageView with tag in Storyboards");
// -> Can't find PFImageView with tag in Storyboards

最后,我在 PFQueryCollectionViewController 中将所有可行的类名组合提供给所有需要类名的方法。 .例如:我尝试了以下代码行,其中我用 PA_ProfileCollectionViewCell 换出了 CLASSNAMEHERE 的所有实例, UICollectionViewCellPFCollectionViewCell , 均等量 失败 :
// the cell (with casting)
CLASSNAMEHERE *cell = (CLASSNAMEHERE *)[cv dequeueReusableCellWithReuseIdentifier:...];

// the cell (without casting)
CLASSNAMEHERE *cell = [cv dequeueReusableCellWithReuseIdentifier:...];

// registering the class
[self.collectionView registerClass:[CLASSNAMEHERE class]
        forCellWithReuseIdentifier:_cellIdentifier];

更新 #01:

根据用户@soulshined 的建议,我尝试跳过 setFile:调用,因为它没有出现在 API via the Parse docs 中(但我从来没有遇到错误,或者使用它时出现 unrecognized selector error),所以我尝试了下面的代码片段,但没有成功(注意如何将 setFile: 替换为 .file :
cell.imageThumb.file = pingThumb;
//[cell.imageThumb setFile:pingThumb];
[cell.imageThumb loadInBackground:^(UIImage *image, NSError *error) {

    if(!error){
        NSLog(@"CODE WON'T REACH THIS POINT.");
        NSLog(@"loadInBackground doesn't exist on cell.imageThumb)");
        [UIView animateWithDuration:0.2f animations:^{
            cell.imageThumb.alpha = 1.0f;
        }];
    }
}];

更新#02

同样,根据@soulshined 的建议,我 used the advice give here ,并更改了我的 PFImageView *imageThumbUIImageView *imageThumb ,并尝试分配从 getDataInBackground:withBlock: 返回的 NSdata到图像,这是我的代码片段(请注意,我确实将“获取数据!”输出到控制台,但图像仍未显示):
// pingThumb is a PFFile
[pingThumb getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            NSLog(@"Got data");
            UIImage *image = [UIImage imageWithData:data];
            [cell.imageThumb setImage:image];
        }
    }];

那么有人能想到我遗漏的一步吗?为什么这么难拿到PFImageView加载 PFCollectionViewCell 的子类?

预先感谢任何人抽出时间帮助我!



Best Answer-推荐答案


您面临的问题是您正在-viewDidLoad 中注册该类(class)。方法,但您正在通过 Storyboard 中的 Prototype 单元设置您的单元。

如果您引用 "Cell and View Reuse" section here ,你会读到(强调我的):

... if the cell or supplementary views are created using prototypes within a storyboard it is not necessary to register the class in code and, in fact, doing so will prevent the cell or view from appearing when the application runs.



这就是为什么您的 PFImageViews行为不正常,因为您在 -registerClass:forCellWithReuseIdentifier: 之后破坏了您在 Storyboards 中建立的 Prototype 连接。被调用,并且您的 PFImageViews 在运行时不确定中丢失了。

因此,要解决您的问题,只需删除 -registerClass:forCellWithReuseIdentifier从您的 View Controller 。

等等,问题解决了...

因此,在您的 View Controller 中的任何地方都不需要下面的代码,因为同样,如果您使用原型(prototype)单元,则无需注册该类:
// Not needed when using Prototype cells in Storyboards!
[self.collectionView registerClass:[PA_ProfileCollectionViewCell class]
            forCellWithReuseIdentifier:_cellIdentifier];

删除 registerClass 代码后,您使用的代码 PFImageView在您问题的代码示例中,上面的示例应该可以正常工作。

希望这可以帮助!

附带说明一下,在 -collectionView:cellForItemAtIndexPathbject 中实例化自定义单元格时无需转换自定义单元格。
// this is all you need
SomeCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:_cellId]; 

// the casting example below is redundant and unnecessary, stick with the top example 
SomeCustomCell *cell = (SomeCustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:_cellId];  

关于ios - PF ImageView 不适用于 PF CollectionViewCell 的子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28860672/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap