[英]UiCollectionView Spacing issue - swift
我的Collection出現間距問題。我不知道為什么。 我希望從第二行到最后一個低點的圖像均勻分布。 有人可以幫我解決這個問題嗎? tnx。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if (indexPath.row == 0) {
return singleUserModel.isevenItems(totalCount: CurrentUser.items.count) ? CGSize(width: self.view.frame.width * 0.4 , height: self.view.frame.height / 4) : CGSize(width: self.view.frame.width , height: self.view.frame.height / 4)
} else {
return CGSize(width: self.view.frame.width * 0.4, height: self.view.frame.height / 4)
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return CurrentUser.items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UserDetailCell", for: indexPath) as! UserDetailCell
configureCell(cell: cell, forRowAtIndexPath: indexPath as NSIndexPath)
return cell
}
func configureCell(cell: UserDetailCell, forRowAtIndexPath indexPath: NSIndexPath) {
DispatchQueue.global(qos: .background).async {
cell.ItemImage.image = self.singleUserModel.loadImage(imageUrl: self.CurrentUser.items[indexPath.row])
cell.activityIndicator.isHidden = true
}
}
視圖看起來像這樣
實際產量
您需要相應地設置布局,現在您的布局將根據屏幕尺寸進行更改,因此您需要根據一種屏幕尺寸進行計算,然后將其用於其他屏幕尺寸:
現在,布局將像這樣。在這2個圖像中將顯示寬度,如果要從左右兩側設置空間,則相應地設置布局寬度。
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if (indexPath.row == 0) {
return singleUserModel.isevenItems(totalCount: CurrentUser.items.count) ? CGSize(width: self.view.frame.width * 0.4 , height: self.view.frame.height / 4) : CGSize(width: self.view.frame.width , height: self.view.frame.height / 4)
} else {
return CGSize(width: (self.view.frame.width/2), height: self.view.frame.height / 4)
}
}
刪除單元格和線條的最小間距,截面插圖也將為零,如下圖所示:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.