Dynamic viewForHeaderInSection and multiple cellForRowAtIndexPath

流过昼夜 提交于 2020-01-17 02:21:48

问题


I'm trying to make a custom cell to be the Section of my cells:

For this I'm overriding some functions like:

numberOfRowsInSection

viewForHeaderInSection

heightForHeaderInSection

And I'm working with multiple cellForRowAtIndexPath I'm using a if indexPath.row to identify the Cell and populate them in a dynamic way.

import UIKit

class TesteTableViewController: PFQueryTableViewController {


override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    loadCollectionViewData()

}


func loadCollectionViewData() {

    // Build a parse query object
    let query = PFQuery(className:"Feed")

    // Check to see if there is a search term


    // Fetch data from the parse platform
    query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in

        // The find succeeded now rocess the found objects into the countries array
        if error == nil {

            print(objects!.count)
            // reload our data into the collection view


        } else {
            // Log details of the failure

        }
    }
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return objects!.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}


override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var header = tableView.dequeueReusableCellWithIdentifier("Cell2")! as! TesteTableViewCell


    return header
}

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 30.0
}


// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!

    // Configure the PFQueryTableView
    self.parseClassName = "Feed"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
}



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell: TesteTableViewCell!
    let object = objects![indexPath.section]
if indexPath.row == 0 {
cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TesteTableViewCell




// Extract values from the PFObject to display in the table cell
if let nameEnglish = object["name"] as? String {
cell?.label1?.text = nameEnglish

}


}
else{
cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! TesteTableViewCell


    // Extract values 2from the PFObject to display in the table cell
    if let nameEnglish2 = object["brief"] as? String {
        cell?.label2?.text = nameEnglish2


    }

}

return cell
}

}

With my code I have this result:

I'm successfully populating both Cells with different Identifiers.

But look like this function is called before the cellForRowAtIndexPath and it return the content that I have in the storyBoard and not the content that I'm populating dynamically.

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var header = tableView.dequeueReusableCellWithIdentifier("Cell2")! as! TesteTableViewCell


    return header
}

And I really think that is my problem. (The order that things happen).

Any ideas?

Thanks.


回答1:


You need to move the code which you have in cellForRowAtIndexPath for row != 0 because that block of code is never executed which is causing static data rendering from storyboard instead of dynamic data.

In this case you have to give dynamic data to cell in viewForHeaderInSection method so that you cell will populate that information on each reload.

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var header = tableView.dequeueReusableCellWithIdentifier("Cell2")! as! TesteTableViewCell
let object = objects![section]
 if let nameEnglish2 = object["brief"] as? String {
        header.label2?.text = nameEnglish2
    }
    return header
}


来源:https://stackoverflow.com/questions/33535442/dynamic-viewforheaderinsection-and-multiple-cellforrowatindexpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!