我有一个来自json的数据列表。有了这个列表,我想把它加载到tableview中,但是在名为Featured和All的两个单独的部分中。试图找出如何让我的“精选”部分不加载与“全部”部分相同的行数。所有部分都很好,但精选部分显示精选列表加上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
}
答案 0 :(得分:0)
您对UITableViewDataSource的实现
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
方法需要提供每个部分要包含的行数。无论部分如何,您的代码都返回相同的行数,根据您描述的内容,这听起来是错误的。您需要计算并返回特色部分的特色项目数,而不是整个数据集的大小。
答案 1 :(得分:0)
&#34; tableView(tableView:UITableView,numberOfRowsInSection section:Int) - &gt; INT&#34;功能在这里。根据部分,您可以将行数返回到此部分
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(section==0){
var count: Int = tableData.count
println("tabledata \(count)")
return count
}else{
return theOtherData.count
}
}
答案 2 :(得分:0)
@tomsoft和@ScorpioCurse我能够通过为数据创建两个单独的数组来获取特色部分以加载正确的数据。在我试图回复那些迅速不喜欢的计数之前。更改它后返回一个单独的数组tableFeatData.count否则返回tableData.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))
{
let featentry: NSMutableDictionary = self.tableFeatData[indexPath.row] as! NSMutableDictionary
var busName = featentry["Business_Name"] as? String
var points = featentry["Member_Points"] as? String
var imageName = featentry["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
{
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
}