簡體   English   中英

UICollectionView iOS8的問題

[英]Problems with UICollectionView iOS8

因此,我遇到的最奇怪的麻煩是我認為應該非常容易。 這是我的收藏夾

所以..如您所見,正確的圖像被剪切掉了。 現在這是奇怪的部分。 這是我的cellForItemAtIndexPath方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 164, 164)]; // 0 0 164 141
    [cell.contentView addSubview:imageView];
    imageView.image = usersPhotos[indexPath.row];

    return cell;
}

這是我的故事板文件中的UICollectionView

請注意 ,我在collectionViewCell故事板文件是155 155,我的ImageView的是164x164。 我這樣做是因為出於某些原因我無法弄清楚:

隨着我的ImageView設置為155x155(大小相同的細胞),我們得到的東西看起來是這樣的:

那我在做什么錯呢? 很明顯,我不希望出現白色間距(如上所示),而且我無法再擴展UICollectionViewCell,因為最大寬度為155,由於情節提要上的某種原因,如果您達到156的寬度,它將使單元格占據整個列, 我沒有足夠的空間來容納第二列

我如何才能簡單地完成這項工作? 非常感謝您的幫助,對於我為什么會遇到這種行為以及我做錯了什么,我非常失落。

在情節提要中,展開視圖控制器,然后選擇“集合視圖流布局”,如下所示:

在此處輸入圖片說明

然后在尺寸檢查器中找到“最小間距”,如下所示:

在此處輸入圖片說明

將“對於單元格”的間距更改為0。這將允許您在單元格之間沒有任何間距的情況下將其放入,並將單元格大小增加到最大160(對於320的屏幕寬度)。

這將我的故事板布局從此更改為160w的單元格大小:

在此處輸入圖片說明 在此處輸入圖片說明

然后,當我運行該應用程序時,所有單元格都在接觸:

在此處輸入圖片說明

現在,您的單元格大小已在情節提要中進行了硬編碼,您必須使其動態化,以便根據您的應用程序在哪個設備上運行而改變。

您必須實現UICollectionViewFlowLayoutDelegate方法,如下所示:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(collectionView.frame.size.width/2, collectionView.frame.size.width/2);
}

您的單元格將需要根據設備的大小以及部分插圖和項目間的間距來調整大小。

在情節提要中,確保在“集合”視圖的“屬性”檢查器中選擇了“流”布局。

然后,實現sizeForItemAtIndexPath並使用部分插圖進行計算:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    // Get the flow layout properties
    UICollectionViewFlowLayout *flowLayout = ((UICollectionViewFlowLayout*)collectionViewLayout);

    // Calculate the usable width for the cell
    CGFloat usableWidth = collectionView.frame.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumInteritemSpacing;

    // Return the proper size
    return CGSizeMake(usableWidth / 2, usableWidth / 2);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM