Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
`titleForHeaderInSection`似乎在iOS中的viewDidLoad之前调用_Ios_Swift_Uitableview_Viewdidload - Fatal编程技术网

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

`titleForHeaderInSection`似乎在iOS中的viewDidLoad之前调用,ios,swift,uitableview,viewdidload,Ios,Swift,Uitableview,Viewdidload,我想在标题中添加标题。这些标题将从在viewDidLoad中启动的数组中检索。我为此使用了覆盖函数标题forheaderinsection。当我运行我的代码时,函数似乎被调用得太早了 我在函数中添加了一个断点,在代码循环3次后,我可以看到我的tableviewcontroller 我还添加了两个打印命令。一个在viewDidLoad中,一个在titleForHeaderInSection中。猜猜看,标题标题标题标题部分的打印是先打印的 此函数不应该在viewDidLoad之后调用吗?我的问题怎么

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

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

我还添加了两个打印命令。一个在
viewDidLoad
中,一个在
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()
}
试试这个

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()
     });
}

viewDidLoad
方法中尝试设置
UITableView的
数据源和
委托
,而不是xib。它可能会工作,只是一个猜测。

显然重新启动Xcode(它意外崩溃)解决了我的问题。现在,在执行
标题forheaderinsection
之前,数组已正确加载

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

我也对代码做了一些修改,但目的应该是一样的。

如果在VC的视图添加到任何内容(如设置委托/数据源)之前,您正在对tableView执行任何操作,那么它将在视图内容之前调用tableView内容谢谢您的解释。我怎样才能确保在那之前我什么都没做?我的其他覆盖函数也基于“viewDidLoad”中的数组,它们工作正常。是否在viewDidLoad中设置tableview?很难说,因为我看不到所有的代码。我的意思是你在哪里配置电视和其他东西,我猜你在viewDidLoad中配置了数据,你考虑过在其他地方做吗?你把
打印
放在
viewDidLoad
的哪里了?你应该知道你正在执行一个后台任务吗?我添加了这个部分,但它仍然没有显示正确的内容。它确实做了一些事情,但在调试器中,我的数组在前3或4个循环中仍然是空的。我不是一个非常有经验的程序员,所以我不太清楚你的意思。我现在已经在编辑中添加了我的代码。没问题。查看此屏幕截图()并在情节提要中选择您的tableview,您是否可以看到
数据源
委托
?设置正确,因此这不应该是问题。酷。我想说的是,请在
self.tableview.dataSource=self
之前,通过使用
self.tableview.dataSource=self
self.tableview.delegate=self
self.tableview.reloadData()方法中取消这两个选项的链接并设置它们。这就是我的屏幕的样子。