问题描述
我正在尝试将自定义单元格设为单元格的部分:
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
viewForHeaderInSection
heightForHeaderInSection
heightForHeaderInSection
我正在使用多个 cellForRowAtIndexPath
我正在使用 if indexPath.row
来标识 Cell
并将它们填充到动态方式.
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.
但是看起来这个函数是在 cellForRowAtIndexPath
之前调用的,它返回我在 storyBoard 中的内容,而不是我动态填充的内容.
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).
有什么想法吗?
谢谢.
推荐答案
您需要将 cellForRowAtIndexPath
中的代码移动到 row != 0
因为那个块的代码永远不会执行,这会导致从故事板而不是动态数据呈现静态数据.
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.
在这种情况下,您必须在 viewForHeaderInSection
方法中为单元格提供动态数据,以便您的单元格在每次重新加载时填充该信息.
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
}
这篇关于动态 viewForHeaderInSection 和多个 cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!