cuốn sách gpt4 ai đã làm

ios - 无法在 collectionView 中调用 dequeueReusableCellWithReuseIdentifier(collectionView : collectionViewLayout: sizeForItemAtIndexPath)

In lại Tác giả: Hồ Xil 更新时间:2023-11-01 02:17:12 28 4
mua khóa gpt4 Nike

hiện hữu collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 中调用 dequeueReusableCellWithReuseIdentifier 时应用崩溃。

注意:这个问题不是重复的,因为 dequeueReusableCellWithReuseIdentifier 在 UICollectionView 的其他地方工作,只是不在特定函数内。

没有异常消息,Xcode 只高亮汇编代码,所以不确定问题出在哪里。

代码如下。

目标是使单元格高度动态,同时宽度与 UICollectionView 的宽度匹配,就像 UITableView 中的单元格一样。

1) 为什么它在 dequeueReusableCellWithReuseIdentifier 上崩溃?

2) 如果您不能使用 dequeueReusableCellWithReuseIdentifier,您还能如何动态确定单元格的固有高度?

3) 是否有更好的方法来模拟 UITableViewCell 的大小属性(即,与 superview 相同的宽度,但动态高度)?有很多关于此主题的 SO 帖子,但没有一个提供非常干净的解决方案。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(MessageCellIdentifier, forIndexPath: indexPath) as! MessageCell

let cellSize = CGSize(width: view.frame.width, height: cell.frame.height)
return cellSize
}

câu trả lời hay nhất

更接近于理解崩溃

这是 UITVC 的 1:1 替换,不会惹恼 Apple。

首先,这是使用回调的地方。

    // if delegate implements size delegate, query it for all items
if (implementsSizeDelegate) {
for (NSInteger item = 0; item < numberOfItems; item++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:(NSInteger)section];
CGSize itemSize = implementsSizeDelegate ? [flowDataSource collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath] : self.itemSize;

PSTGridLayoutItem *layoutItem = [layoutSection addItem];
layoutItem.itemFrame = (CGRect){.size=itemSize};
}

https://github.com/steipete/PSTCollectionView/blob/master/PSTCollectionView/PSTCollectionView.m

Kiểm tra属性 分配和最后一行...

- (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
// de-queue cell (if available)
NSMutableArray *reusableCells = _cellReuseQueues[identifier];
PSTCollectionViewCell *cell = [reusableCells lastObject];
PSTCollectionViewLayoutAttributes *attributes = [self.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];

// ... IMPL ...

[cell applyLayoutAttributes:attributes];

return cell;
}

有很多代码要经过,但至少有一条路径可以使您陷入无限递归...

biên tập

从我在下面发布的链接...

Since collectionView:layout:sizeForItemAtIndexPath: is called before cellForItemAtIndexPath:, so we need to initialize a cell and let system use auto layout to calculate height for us. To avoid memory leak, we use a dictionary to cache the cells that are off screen (not shown on screen)

在collectionView:layout:sizeForItemAtIndexPath:中,首先创建或获取一个cell

var cell: MyCollectionViewCell? = self.offscreenCells[reuseIdentifier] as? MyCollectionViewCell
if cell == nil {
cell = NSBundle.mainBundle().loadNibNamed("MyCollectionViewCell", owner: self, options: nil)[0] as? MyCollectionViewCell
self.offscreenCells[reuseIdentifier] = cell
}

cell一旦初始化,其大小由xib文件中的size决定,因此我们需要在cell和layoutSubviews中配置text,让系统重新计算cell的大小

// Config cell and let system determine size
cell!.configCell(titleData[indexPath.item], content: contentData[indexPath.item], titleFont: fontArray[indexPath.item] as String, contentFont: fontArray[indexPath.item] as String)
// Cell's size is determined in nib file, need to set it's width (in this case), and inside, use this cell's width to set label's preferredMaxLayoutWidth, thus, height can be determined, this size will be returned for real cell initialization
cell!.bounds = CGRectMake(0, 0, targetWidth, cell!.bounds.height)
cell!.contentView.bounds = cell!.bounds

// Layout subviews, this will let labels on this cell to set preferredMaxLayoutWidth
cell!.setNeedsLayout()
cell!.layoutIfNeeded()

Once cell is updated, call var size = cell!.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) to get the size for this cell.

In cellForItemAtIndexPath:, cell also need configured and layout its subviews

原创

嗯,调用你命名的方法时不崩溃(一半),调用时崩溃

dequeueReusableCellWithReuseIdentifier(MessageCellIdentifier, 
forIndexPath: indexPath) as! MessageCell

我看到的问题是您正在尝试调用一个方法,该方法需要您调用它的方法来给您一个答案。它应该会产生堆栈溢出。

我并不是说这是不可能的,但你没有足够的压倒性或我认为正确的方法。

Kiểm tra https://github.com/honghaoz/Dynamic-Collection-View-Cell-With-Auto-Layout-Demo/blob/master/README.md获取您想要的示例,和/或发布更多代码。

关于ios - 无法在 collectionView 中调用 dequeueReusableCellWithReuseIdentifier(collectionView : collectionViewLayout: sizeForItemAtIndexPath),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36393044/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress