gpt4 book ai didi

具有不同表数据的 ios swift uitableview 部分

转载 作者:行者123 更新时间:2023-11-30 14:06:41 25 4
gpt4 key购买 nike

我有一个来自 json 的数据列表。有了这个列表,我想将其加载到表格 View 中,但在两个单独的部分中,称为“精选”和“全部”。试图找出如何让我的“精选”部分不加载与“全部”部分相同数量的行。所有部分都很好,但精选部分显示精选列表加上 16 个空行。关于如何删除精选部分中的这些额外行有什么想法吗?

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if(section == 0)
{
return "Featured"
}
return "All"
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
var sections: Int = 2
return sections
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count: Int = tableData.count
println("tabledata \(count)")

return count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//let session = NSURLSession.sharedSession()
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
let entry : NSMutableDictionary = self.tableData[indexPath.row] as! NSMutableDictionary

var featured = entry["Business_IsFeatured"] as? String

if ((featured == "1") && (indexPath.section == 0))
{
var busName = entry["Business_Name"] as? String
var points = entry["Member_Points"] as? String
var imageName = entry["Business_Image"] as? String
var imgURL: NSURL = NSURL(string: "http://www.example.com/images/\(imageName!)")!
let request: NSURLRequest = NSURLRequest(URL: imgURL)
NSURLConnection.sendAsynchronousRequest(
request, queue: NSOperationQueue.mainQueue(),
completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
if error == nil {
cell.textLabel!.text = busName
cell.textLabel!.numberOfLines = 0
cell.detailTextLabel!.text = "Points: \(points!)"
cell.imageView!.image = UIImage(data: data)
}
})
}
else
{
if((featured == "0") && (indexPath.section == 1))
{
var busName = entry["Business_Name"] as? String
var points = entry["Member_Points"] as? String
var imageName = entry["Business_Image"] as? String
var imgURL = NSURL(string: "http://www.example.com/images/\(imageName!)")

cell.textLabel!.text = busName
cell.textLabel!.numberOfLines = 0
cell.detailTextLabel!.text = "Points: \(points!)"
cell.imageView!.hnk_setImageFromURL(imgURL!, format: Format<UIImage>(name: "original"))
}
}

return cell
}

最佳答案

您的 UITableViewDataSource 的实现

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

方法需要提供您想要每个部分包含的行数。无论部分如何,您的代码都会返回相同的行数,根据您描述想要执行的操作的方式,这听起来是错误的。您需要计算并返回“精选”部分的精选项目数量,而不是整个数据集的大小。

关于具有不同表数据的 ios swift uitableview 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32301280/

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