gpt4 book ai didi

ios - IGListKit 从 Alamofire 请求向类插入数据

转载 作者:行者123 更新时间:2023-11-28 12:31:02 25 4
gpt4 key购买 nike

所以我有这个模型

class Event: NSObject {
var _eventName: String!
var _venueName : String!
var _eventImage: String!


var eventName: String {
if _eventName == nil {
_eventName = ""
}
return _eventName
}

var venueName: String {
if _venueName == nil {
_venueName = ""
}
return _venueName
}

var eventImage: String {
if _eventImage == nil {
_eventImage = ""
}
return _eventImage
}

init(eventsDict: Dictionary<String, AnyObject>) {
if let venue = eventsDict["venue"] as? Dictionary<String, AnyObject> {
if let venuname = venue["name"] as? String{
self._venueName = venuname
}
if let eventname = eventsDict["name"] as? String {
self._eventName = eventname
}
if let eventimage = eventsDict["coverPicture"] as? String {
self._eventImage = eventimage
}
}
}

我用这个扩展使它成为 IGListDiffable。

extension NSObject: IGListDiffable {

public func diffIdentifier() -> NSObjectProtocol {
return self
}

public func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
return isEqual(object)
}
}

所以当我像这样从硬编码代码加载数据时

var entries = [Event]()
func loadFakeEvents() {
let entries = [
Event(
eventName: "Ζωρζ Πιλαλι Και Η Soufra Band Στο AN Groundfloor - Live Stage!",
venueName: "AN Groundfloor - live stage",
eventImage: "https://scontent.xx.fbcdn.net/v/t31.0-8/s720x720/15936729_1867160333520142_8855370744955080264_o.jpg?oh=8198bc10a8ea61011d7ec1902b34aa01&oe=593D6BC4"
),
Event(
date: "2017-02-18T21:30:00+0200",
name: "Διονύσης Σαββόπουλος at Gazarte I Main Stage 18/02",
venuename: "Gazarte",
eventImage: "https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/16265335_1262826863809003_3636661375515976849_n.jpg?oh=5bb342321a65d33dbc1cc41de266b45e&oe=5907857C"
)
]
self.entries = entries
}

事件加载正常。因为他们必须这样做。

但是当我发出 alamofire 请求时,当然需要一些时间来加载数据并将它们附加到空的事件数组中。

这是我必须调用事件的函数

    func loadEvents() {

let parameters: Parameters = [
"Some" : "Parameters",
"Some" : "Parameters"
]
Alamofire.request(baseurl, method: .get, parameters: parameters)
.responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let result = responseData.result

if let dict = result.value as? Dictionary<String, AnyObject>{
print(dict) // <-- Check this out
if let list = dict["events"] as? [Dictionary<String, AnyObject>] {

for obj in list {
let event = Event(eventsDict: obj)
self.entries.append(event)

}
}
}
}
}
}

所以在上面的代码中我有一个 print,它打印 json。

在我的

extension LocationViewController: IGListAdapterDataSource {
func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] {
let items: [IGListDiffable] = loader.entries as [IGListDiffable]
print(items.count) // <--- another print of items that should be displayed
return items
}
func listAdapter(_ listAdapter: IGListAdapter, sectionControllerFor object: Any) -> IGListSectionController {
return NormalSectionController()
}
func emptyView(for listAdapter: IGListAdapter) -> UIView? { return nil }
}

Adapter i 还打印应该显示的项目。因此,当我加载 fakeEvents 函数时,它会打印 2 但是当我使用普通函数加载它们时,它会打印 0 然后是 JSON 来自 dict 来自先前代码的 var。

通常我会 reloadData() Collection View 。但是使用 IGListKit 将事件类发送到 CollectionView 的技巧是什么?非常感谢您抽出时间,我希望我没有跑题!

最佳答案

在 Github 上粘贴我在同一问题上的回答,以防有人找到。

https://github.com/Instagram/IGListKit/issues/468

当附加到条目字典时,您似乎在 for 循环之后错过了对 self.adapter.performUpdates(animated: true) 的调用:

func loadEvents() {
// ...
Alamofire.request(baseurl, method: .get, parameters: parameters)
.responseJSON { (responseData) -> Void in

if responseData.result.value != nil {
let result = responseData.result
if let dict = result.value as? Dictionary<String, AnyObject>{
if let list = dict["events"] as? [Dictionary<String, AnyObject>] {
for obj in list {
let event = Event(eventsDict: obj)
self.entries.append(event)
}
// missing this!
self.adapter.performUpdates(animated: true)
// missing that!
}
}
}
}
}

关于ios - IGListKit 从 Alamofire 请求向类插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42094211/

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