动态viewForHeaderInSection和多个cellForRowAtIndexPath

时间:2015-11-05 02:04:48

标签: ios swift uitableview parse-platform

我正在尝试将自定义单元格设置为我的单元格部分:

enter image description here

为此,我重写了一些功能,如:

  

numberOfRowsInSection

     

viewForHeaderInSection

     

heightForHeaderInSection

我正在使用多个cellForRowAtIndexPath我正在使用if indexPath.row来识别Cell并以动态方式填充它们。

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
}

}

使用我的代码我得到了这个结果:

enter image description here

我已成功填充具有不同标识符的两个单元格。

但看起来这个函数在cellForRowAtIndexPath之前被调用,它返回我在storyBoard中的内容,而不是我正在动态填充的内容。

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


    return header
}

我真的认为这是我的问题。 (事情发生的顺序)。

有什么想法吗?

感谢。

1 个答案:

答案 0 :(得分:1)

您需要移动cellForRowAtIndexPathrow != 0所拥有的代码,因为该代码块永远不会被执行,导致从故事板而不是动态数据呈现静态数据。

在这种情况下,您必须使用viewForHeaderInSection方法向单元格提供动态数据,以便您的单元格将在每次重新加载时填充该信息。

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
}
相关问题