最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中TableView的刷新数据reloadData有时候不生效

Swift crifan 6420浏览 0评论
把swift的代码中的http请求换成异步的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
func getUrlRespJsonDict_async(url:String, extraParas:AnyObject?, respJsonDictHandler:((Bool, AnyObject?, JSON) -> Void)) {
    print("url=\(url)")
 
    var getJsonOk:Bool = false
    let params:Dictionary<String, AnyObject>? = nil
 
    do {
        let opt = try HTTP.GET(
            url,
            parameters: params,
            requestSerializer: JSONParameterSerializer())
 
        opt.start { response in
            if let err = response.error {
                print("url=\(url) with params=\(params) response error= \(err.localizedDescription)")
 
                getJsonOk = false
                let errJsonDict = JSON(["error" : err.localizedDescription])
                respJsonDictHandler(getJsonOk, extraParas, errJsonDict)
                return
            }
 
            //print("response.statusCode=\(response.statusCode)")
            //print("opt finished: \(response.description)")
 
            let decodedJsonDict:JSON = JSON(data: response.data)
            print("decodedJsonDict=\(decodedJsonDict)")
 
            getJsonOk = true
            respJsonDictHandler(getJsonOk, extraParas, decodedJsonDict)
        }
    } catch let httpGetError {
        print("url=\(url) with params=\(params) got error= \(httpGetError)")
 
        getJsonOk = false
        let errJsonDict = JSON(["error" : "\(httpGetError)"])
        respJsonDictHandler(getJsonOk, extraParas, errJsonDict)
    }
}
 
   func contactIdListJsonDictHandler(getJsonOk:Bool, extraParas:AnyObject?, respJsonDict:JSON){
 
        if getJsonOk {
            var contactIdList:[String] = [String]()
            contactIdList = respJsonDict.arrayObject as! [String]
            print("contactIdList=\(contactIdList)") //
 
            let userTeamInfoDict:[String:String] = extraParas as! [String:String]
            let userIdStr:String = userTeamInfoDict["userIdStr"]! as String
            let teamIdStr:String = userTeamInfoDict["teamIdStr"]! as String
 
            let totalContactNum:Int = contactIdList.count
 
            for eachContactIdStr in contactIdList {
                let getContactInfoUrl:String = StrServerUrl + "/user/" + userIdStr + "/teams/joined/" + teamIdStr + "/members/" + eachContactIdStr
                getUrlRespJsonDict_async(getContactInfoUrl, extraParas: totalContactNum, respJsonDictHandler: singleContactItemJsonDictHandler)
            }
        }
    }
 
    func singleContactItemJsonDictHandler(getJsonOk:Bool, extraParas:AnyObject?, respJsonDict:JSON?){
        if getJsonOk {
            var singleContactItem:ContactItem = ContactItem()
            singleContactItem.nameStr = respJsonDict!["name"].string!
            //singleContactItem.phoneNumberStr = respJsonDict!["phone"].stringValue
 
            print("singleContactItem=\(singleContactItem)")
 
            insertContactItemToList(singleContactItem)
 
            //TODO: reload tableview data for each contact item ?
 
            let totalContactNum:Int = extraParas as! Int
            if getCurTotalContactItemNum() >= totalContactNum {
                //reload contact tableView
                print("gContactViewController=\(gContactViewController)")
                print("gContactViewController.contactTableView=\(gContactViewController!.contactTableView)")
                //gContactViewController?.contactTableView.reloadData()
                gContactViewController!.contactTableView.reloadData()
            }
        }
    }
之后,经常遇到如下问题:
希望是
调用了:
1
gContactViewController!.contactTableView.reloadData()
后,联系人列表,会刷新为:
table view has update reload data
但是
此处,经常遇到:
reloadData没生效,而联系人列表是旧的内容:
reload not work still old contacts
而没有刷新。
搜:
swift tableview reloaddata sometime not work
swift async tableview reloaddata sometime not work
参考:
去试试:
1
2
3
4
dispatch_async(dispatch_get_main_queue(), { () -> Void in
    //gContactViewController?.contactTableView.reloadData()
    gContactViewController!.contactTableView.reloadData()
})
然后真的可以了,正常,立刻,及时的刷新数据,页面上的联系人保持最新的值。
[总结]

转载请注明:在路上 » [已解决]swift中TableView的刷新数据reloadData有时候不生效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.310 seconds, using 22.29MB memory