gpt4 book ai didi

ios - 使用转义闭包对AlamoFire中的UITableViewData进行排序

转载 作者:行者123 更新时间:2023-12-01 16:08:37 25 4
gpt4 key购买 nike

我正在尝试按字母顺序对带有自定义单元格的UTableView进行排序和分段。在我的ViewController中,我有一个下载JSON数据,然后将其存储在Dictionary中的函数。

var attendees = [Attendee]()
var sortedFirstLetters: [String] = []
var sections: [[Attendee]] = [[]]

func downloadAttendeeData(completed: @escaping DownloadComplete) {
let headers: HTTPHeaders = [

"Accept": "application/json;odata=verbose",
"User-Agent": "iOS;iOS"

]

NetworkManager.sharedInstance.manager.request(Attendee_URL, method: .get, headers: headers)
.responseJSON { response in

let result = response.result

if let dict = result.value as? Dictionary<String, AnyObject> {

if let list = dict["d"]?["results"] as? [Dictionary<String, AnyObject>] {

for att in list {
let attendee = Attendee(AttendeeDict: att)
self.attendees.append(attendee)

}


self.tableView.reloadData()

}

}

completed()
}


}

这段代码正是我所需要的

    let firstLetters = self.attendees.map { $0.titleFirstLetter }
let uniqueFirstLetters = Array(Set(firstLetters))
self.sortedFirstLetters = uniqueFirstLetters.sorted()

self.sections = self.sortedFirstLetters.map { firstLetter in
return self.names
.filter { $0.titleFirstLetter == firstLetter }
.sorted { $0.titleAD < $1.titleAD }
}

但是,当我将这段代码放在viewDidLoad中时,它会生成和清空数组。

override func viewDidLoad() {
super.viewDidLoad()


tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self

let firstLetters = self.attendees.map { $0.titleFirstLetter }
let uniqueFirstLetters = Array(Set(firstLetters))
self.sortedFirstLetters = uniqueFirstLetters.sorted()

self.sections = self.sortedFirstLetters.map { firstLetter in
return self.names
.filter { $0.titleFirstLetter == firstLetter }
.sorted { $0.titleAD < $1.titleAD }
}

self.downloadAttendeeData {


}



}

如果我将代码放入self.downloadAttendeeData声明中,它将产生数据,但不会创建表节。

将数据加载到tableView后,如何生成节?那是问题吗?

最佳答案

确保从主线程调用tableView.reloadData()像这样:

dispatch_async(dispatch_get_main_queue()) { 
self.tableView.reloadData()
}

或在Swift 3及更高版本中:
DispatchQueue.main.async {
self.tableView.reloadData()
}

关于ios - 使用转义闭包对AlamoFire中的UITableViewData进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46065134/

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