我想在标题中添加标题。这些标题将从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()
}
答案 0 :(得分:0)
试试这个......
override func viewWillAppear(animated: Bool) {
super. viewWillAppear (animated);
double delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//code to be executed on the main queue after delay
self.tableView.reloadData()
});
}
答案 1 :(得分:0)
在UITableView's
方法中尝试设置dataSource
delegate
和viewDidLoad
而不是xib。它可能有用,只是猜测。
答案 2 :(得分:0)
显然重新启动Xcode(它意外崩溃)解决了我的问题。现在,在执行titleForHeaderInSection
之前,数组已正确加载。
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
var kopTekst = ""
for index in tasks.indices{
if section == index{
kopTekst = tasks[index]
}
}
return kopTekst
}
我也改变了一些代码,但意图应该是一样的。