菜鸟教程小白 发表于 2022-12-13 05:37:54

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


                                            <p>我很难获得 <code>PFCollectionViewCell</code> 的子类和 <code>PFImageView</code> 玩得很好. <strong>我正在使用 Storyboards 来装配我的 PA_ProfileCollectionViewCell。 </strong><br><br>我想要做的就是加载一个图像(我可以从解析中成功获得),并将其分配给 <code>PFImageView</code>在我的自定义单元格中。我会很感激任何可以帮助我用棒球棒砸我的电脑,并在慢动作的荣耀中摧毁我的办公室的任何帮助/见解。<br><br> <strong>我想要的结果:</strong>让 <code>PFCollectionViewCell</code> 使用自定义单元格( <code>-collectionView:cellForItemAtIndexPath:object:</code> 的子类)<br><br> <strong>问题:</strong>我的自定义属性 <code>PFImageView *imageThumb</code> (在我的子类 <code>PFCollectionViewCell</code> 上)不响应 <code>loadInBackground:</code>或 <code>setFile:</code>这是 <code>PFImageView</code> 上的两种方法类(class)。<br><br>为了清楚起见,这是我的代码:<br><br> <strong> - 我的自定义 PFCollectionViewCell:<code>ProfileCollectionViewCell</code> </strong><br><pre><code>@interface PA_ProfileCollectionViewCell : PFCollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageThumb;
@end
</code></pre><br> <strong> - 在 vi​​ewDidLoad 中注册自定义类 </strong><br><pre><code>- (void)loadView {
    ;
   
            forCellWithReuseIdentifier:_cellIdentifier];
}
</code></pre><br> <strong> - 在 <code>collectionView:cellForItemAtIndexPath:object</code> 中设置单元格</strong><br>这是主要问题发生的地方。根据 <code>cell.backgroundColor:</code>,我的单元格值已成功绘制为深灰色调用,但所有将图像分配给我的 imageThumb 的尝试都失败了。请注意,在下面的源代码中,我的 <code>imageThumb</code>不响应选择器 <code>loadInBackground</code>或 <code>setFile</code> , 附带 <code>PFImageView</code> .<br><pre><code>-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
                                  object:(PFObject *)object {


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

    // Load the photo
    PFFile *pingThumb = ;
    if (pingThumb){

      BOOL canLoadInBackground = ;
      BOOL canSetFile = ;

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

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

    }

    return cell;
}
</code></pre><br> <strong>输出所有<code>NSLog()</code>上面代码片段中的语句是:</strong><br><pre><code>can cell.imageThumb loadInBackground? : 0 (NO)
can cell.imageThumb setFile? : 0 (NO)
</code></pre><br>我尝试过的其他事情:<br><br>我已经选择了 <a href="https://github.com/ParsePlatform/ParseUI-iOS" rel="noreferrer noopener nofollow">ParseUIDemo</a> 上的源代码,但他们所有的实现都使用股票 <code>PFCollectionViewCell</code>没有任何自定义或子类化,所以我尝试调整我的项目以使用他们的方法,但我也永远无法让事情在那里工作。<br><br>我已通读 <a href="https://stackoverflow.com/a/21616308/3960969" rel="noreferrer noopener nofollow">this answer</a> , 在 SO 上,这让我尝试爬过我的 <code>cell.contentViews.subviews</code>和 <code>cell.subviews</code>寻找<code>PFImageView</code>两次尝试都没有成功:<br><pre><code>//
for (UIView *subview in cell.contentView.subviews) {
    if (]) {
      NSLog(@&#34;Yay! I found a PFImageView&#34;); // Never gets called
      break;
    }
}
</code></pre><br>然后我尝试了 <a href="https://stackoverflow.com/a/21616495/3960969" rel="noreferrer noopener nofollow">this answer</a>在 SO 上,这让我尝试获取 <code>PFImageView</code> 的实例从我的 Storyboard使用标签,这同样不成功。<br><pre><code>PFImageView *photoView = (PFImageView *);
if(!photoView) NSLog(@&#34;Can&#39;t find PFImageView with tag in Storyboards&#34;);
// -&gt; Can&#39;t find PFImageView with tag in Storyboards
</code></pre><br>最后,我在 <code>PFQueryCollectionViewController</code> 中将所有可行的类名组合提供给所有需要类名的方法。 .例如:我尝试了以下代码行,其中我用 <code>PA_ProfileCollectionViewCell</code> 换出了 CLASSNAMEHERE 的所有实例, <code>UICollectionViewCell</code>和 <code>PFCollectionViewCell</code> , 均等量 <strong>失败 </strong> :<br><pre><code>// the cell (with casting)
CLASSNAMEHERE *cell = (CLASSNAMEHERE *);

// the cell (without casting)
CLASSNAMEHERE *cell = ;

// registering the class

      forCellWithReuseIdentifier:_cellIdentifier];
</code></pre><br>更新 #01:<br><br>根据用户@soulshined 的建议,我尝试跳过 <code>setFile:</code>调用,因为它没有出现在 <a href="https://parse.com/docs/ios/api/Classes/PFImageView.html" rel="noreferrer noopener nofollow">API via the Parse docs</a> 中(但我从来没有遇到错误,或者使用它时出现 <code>unrecognized selector error</code>),所以我尝试了下面的代码片段,但没有成功(注意如何将 <code>setFile:</code> 替换为 <code>.file</code> :<br><pre><code>cell.imageThumb.file = pingThumb;
//;
[cell.imageThumb loadInBackground:^(UIImage *image, NSError *error) {

    if(!error){
      NSLog(@&#34;CODE WON&#39;T REACH THIS POINT.&#34;);
      NSLog(@&#34;loadInBackground doesn&#39;t exist on cell.imageThumb)&#34;);
      [UIView animateWithDuration:0.2f animations:^{
            cell.imageThumb.alpha = 1.0f;
      }];
    }
}];
</code></pre><br>更新#02<br><br>同样,根据@soulshined 的建议,我 <a href="https://parse.com/questions/how-to-convert-pffile-to-uiimage-on-ios-5" rel="noreferrer noopener nofollow">used the advice give here</a> ,并更改了我的 <code>PFImageView *imageThumb</code>到 <code>UIImageView *imageThumb</code> ,并尝试分配从 <code>getDataInBackground:withBlock:</code> 返回的 NSdata到图像,这是我的代码片段(请注意,我确实将“获取数据!”输出到控制台,但图像仍未显示):<br><pre><code>// pingThumb is a PFFile
[pingThumb getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
      if (!error) {
            NSLog(@&#34;Got data&#34;);
            UIImage *image = ;
            ;
      }
    }];
</code></pre><br>那么有人能想到我遗漏的一步吗?为什么这么难拿到<code>PFImageView</code>加载 <code>PFCollectionViewCell</code> 的子类?<br><br>预先感谢任何人抽出时间帮助我!</p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p>您面临的问题是您正在<code>-viewDidLoad</code> 中注册该类(class)。方法,但您正在通过 Storyboard 中的 Prototype 单元设置您的单元。<br><br>如果您引用 <a href="http://www.techotopia.com/index.php/An_Overview_of_iOS_6_Collection_View_and_Flow_Layout" rel="noreferrer noopener nofollow">&#34;Cell and View Reuse&#34; section here</a> ,你会读到(强调我的):<br><br> <blockquote>
<p>... 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, <strong>doing so will prevent the cell or view from appearing when the
application runs</strong>.</p>
</blockquote><br><br>这就是为什么您的 <code>PFImageViews</code>行为不正常,因为您在 <code>-registerClass:forCellWithReuseIdentifier:</code> 之后破坏了您在 Storyboards 中建立的 Prototype 连接。被调用,并且您的 PFImageViews 在运行时不确定中丢失了。<br><br> <strong>因此,要解决您的问题,只需删除 <code>-registerClass:forCellWithReuseIdentifier</code>从您的 ViewController 。 </strong><br><br>等等,问题解决了...<br><br>因此,在您的 ViewController 中的任何地方都不需要下面的代码,因为同样,如果您使用原型(prototype)单元,则无需注册该类:<br><pre><code>// Not needed when using Prototype cells in Storyboards!

            forCellWithReuseIdentifier:_cellIdentifier];
</code></pre><br>删除 registerClass 代码后,您使用的代码 <code>PFImageView</code>在您问题的代码示例中,上面的示例应该可以正常工作。<br><br> <strong>希望这可以帮助! </strong><br><br>附带说明一下,在 <code>-collectionView:cellForItemAtIndexPath:object</code> 中实例化自定义单元格时无需转换自定义单元格。<br><pre><code>// this is all you need
SomeCustomCell *cell = ;

// the casting example below is redundant and unnecessary, stick with the top example
SomeCustomCell *cell = (SomeCustomCell *);
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - PF ImageView 不适用于 PF CollectionViewCell 的子类,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/28860672/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/28860672/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - PF ImageView 不适用于 PF CollectionViewCell 的子类