gpt4 book ai didi

ios - `titleForHeaderInSection` 似乎在 iOS 中的 viewDidLoad 之前被调用

转载 作者:行者123 更新时间:2023-11-30 13:31:09 24 4
gpt4 key购买 nike

我想在标题中添加标题。这些标题将从在 viewDidLoad 中启动的数组中检索,为此我使用了override func titleForHeaderInSection。当我运行我的代码时,该函数似乎被调用得太早了。

我在函数中添加了一个断点,在循环执行代码 3 次后,我能够看到我的 tableviewcontroller

我还添加了两个打印命令。一个在 viewDidLoad 中,一个在 titleForHeaderInSection 中。你猜怎么着,首先打印了 titleForHeaderInSection 打印内容。

这个函数不应该在viewDidLoad之后调用吗?我的问题如何解决?

代码如下:

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
print("title")
for index in tasks.indices{
print(index)
if section == index{
print(tasks[index])
return tasks[index]
}
return "no name specified"
}
return ""

}

数组只是一个简单的 [a,b,c,d],现在第一部分的标题是“a”,其他 3 部分的标题是“未指定名称”

我的 viewDidLoad 方法如下所示:

var taskName = [""]
var dateStamp = [""]
var userId = [""]
var clType = [""]
var tasks = [""]
var count = 0
var countarr = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
var result = false
var dupClType = [""]
var loaded = false
override func viewDidLoad() {
super.viewDidLoad()

let query = PFQuery(className:"pendingTasks")
query.orderByAscending("clType")
query.findObjectsInBackgroundWithBlock({ (object: [PFObject]?, error: NSError?) in
if error != nil{

print ("error")
} else {
if let objects = object{
self.taskName.removeAll(keepCapacity: true)
self.dateStamp.removeAll(keepCapacity: true)
self.userId.removeAll(keepCapacity: true)
self.clType.removeAll(keepCapacity: true)

for object in objects{
if object.objectId != "Q8knzhSDon"{
self.taskName.append(object.objectForKey("taskName") as! String)
self.dateStamp.append(object.objectForKey("dateStamp") as! String)
self.userId.append(object.objectId! as String)
self.clType.append(object.objectForKey("clType") as! String)

}

}


}


self.tasks = Array(Set(self.clType))
self.tasks.sortInPlace()
self.loaded = true
for items in self.tasks.indices{
self.count=0
for item in self.clType{
if item == self.tasks[items]{
self.count = self.count + 1
self.countarr[items] = self.count

}
}

}

while self.countarr.last == 0{
self.countarr.removeLast()

}

self.dupClType = self.clType
}
self.tableView.reloadData()

})

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

最佳答案

尝试在viewDidLoad方法中设置UITableView的dataSourcedelegate而不是xib。它可能有效,只是猜测。

关于ios - `titleForHeaderInSection` 似乎在 iOS 中的 viewDidLoad 之前被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36573299/

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