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

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

Swift crifan 6293浏览 0评论
把swift的代码中的http请求换成异步的:
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
                print("getContactInfoUrl=\(getContactInfoUrl)") //http://192.168.1.110:8080/user/user-7aa7e3c3-bf8f-46e1-8cbe-3a5aad6e9dfb/teams/joined/team-b6c09caa-4e08-48d7-a415-ef1f69f8c588/members/user-22719dc9-7f4b-47ab-807d-9b2146b1344a
                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()
            }
        }
    }
之后,经常遇到如下问题:
希望是
调用了:
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
参考:
去试试:
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    //gContactViewController?.contactTableView.reloadData()
                    gContactViewController!.contactTableView.reloadData()
                })
然后真的可以了,正常,立刻,及时的刷新数据,页面上的联系人保持最新的值。
[总结]

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

发表我的评论
取消评论

表情

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

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