gpt4 book ai didi

ios - 如果 Collection View 单元格中的计数 > 8,则将额外的单元格添加为 "see more"

转载 作者:行者123 更新时间:2023-11-29 01:01:49 26 4
gpt4 key购买 nike

我有一个 UICollectionView。我从我的 URL 动态显示我的 Collection View 中的单元格。我设置了单元格的宽度和高度,只有 4 个单元格应该在单行中。所以现在从我的网址中,我总共得到了 14 件商品。我的意思是该标签有 14 个标签名称和 14 个图像。所以我的表格 View 中总共需要显示 14 个项目。

但我需要的是:我只需要在我的 Collection View 中显示两行,并为单元格设置宽度和高度。所以每行 4 项意味着,两行总共 8 项。但是从我的 URL 中我得到了 14 个项目,对吗?

所以,我需要的是 - 如果项目数大于 7。我从 0 到 7 计数。然后我需要将第 7 个单元格显示为静态。文本应自动更改为“查看更多” .

怎么做?

import UIKit

class HomeViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate {


@IBOutlet weak var collectionView1: UICollectionView!


var BTdata = [BTData]()


override func viewDidLoad()
{
super.viewDidLoad()

ListBusinessTypes()

}

// Values from Api for Business Types
func ListBusinessTypes()
{
let token = NSUserDefaults.standardUserDefaults().valueForKey("access_token") as! String

let headers = ["x-access-token": token]

let request = NSMutableURLRequest(URL: NSURL(string: “some url“)!,
cachePolicy: .UseProtocolCachePolicy,
timeoutInterval: 10.0)
request.HTTPMethod = "GET"
request.allHTTPHeaderFields = headers

let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if (error != nil)
{
print(error)

let ErrorAlert = UIAlertController(title: "Error", message: "Problem with internet connectivity or server, please try after some time", preferredStyle: UIAlertControllerStyle.Alert)

// add an action (button)
ErrorAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

// show the alert
self.presentViewController(ErrorAlert, animated: true, completion: nil)
}
else
{
if let json = (try? NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? Dictionary<String,AnyObject>
{
let success = json["success"] as? Int

if(success == 1)
{

if let typeValues = json["data"] as? [NSDictionary]
{
dispatch_async(dispatch_get_main_queue(),{

for item in typeValues
{
self.BTdata.append(BTData(json:item))
}

self.collectionView1!.reloadData()
})
}
}
else
{
let message = json["message"] as? String

print(message)

let ServerAlert = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert)

// add an action (button)
ServerAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

// show the alert
self.presentViewController(ServerAlert, animated: true, completion: nil)
}
}
}
})

dataTask.resume()
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return BTdata.count

}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{

let cell: collview1 = collectionView.dequeueReusableCellWithReuseIdentifier("Cell1", forIndexPath: indexPath) as! collview1
cell.lblCellA.text = BTdata[indexPath.row].BTNames

cell.imgCellA.image = UIImage(named: tableImages[indexPath.row])


return cell


}




collection view cell space and size
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{


return CGSizeMake((self.view.frame.size.width/4) - 10, (self.view.frame.size.width/4) - 15);

}



}

最佳答案

首先不返回BTdata.count,如果大于8则返回8。

return BTdata.count>8 ?  8 : BTdata.count;

接下来在 UI 编辑器中添加另一种类型的单元格,为其命名(比如 SeeMore,并修改 Collection View 项处理程序,以便它为最后一行返回适当的单元格:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if indexPath.row==7 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("SeeMore", forIndexPath: indexPath)
return cell
} else {
let cell: collview1 = collectionView.dequeueReusableCellWithReuseIdentifier("Cell1", forIndexPath: indexPath) as! collview1
cell.lblCellA.text = BTdata[indexPath.row].BTNames
cell.imgCellA.image = UIImage(named: tableImages[indexPath.row])
return cell
}
}

并在 collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 方法中相应地返回该单元格的适当大小。

关于ios - 如果 Collection View 单元格中的计数 > 8,则将额外的单元格添加为 "see more",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36912972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com