Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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
Ios 使用异步数据更新TableViewCell_Ios_Swift_Uitableview_Swift2 - Fatal编程技术网

Ios 使用异步数据更新TableViewCell

Ios 使用异步数据更新TableViewCell,ios,swift,uitableview,swift2,Ios,Swift,Uitableview,Swift2,我有一个tableViewCell,它包含8个图像,共分为两个块(每个块中有4个图像)。这些图像以异步方式下载并存储到一个数组中,然后在tableViewCell的cellForRowAtIndexPath中用于填充图像。当一个块的所有图像都添加到字典(groupTOImages)中的数组时,我重新加载tableView。按照我的方式,我得到的结果与加载数据的顺序不一致。有些图像被加载到不该加载的位置。是否有办法下载图像并在tableViewCell中获得一致的结果 var groupNames

我有一个tableViewCell,它包含8个图像,共分为两个块(每个块中有4个图像)。这些图像以异步方式下载并存储到一个数组中,然后在tableViewCell的
cellForRowAtIndexPath
中用于填充图像。当一个块的所有图像都添加到字典(groupTOImages)中的数组时,我重新加载tableView。按照我的方式,我得到的结果与加载数据的顺序不一致。有些图像被加载到不该加载的位置。是否有办法下载图像并在tableViewCell中获得一致的结果

var groupNames = [NSManagedObject]()
var groupTOPeople = [NSManagedObject: [String]]()
var groupTOImages = [NSManagedObject: [UIImage]]()
func getGroups() {
     ...
     for group in groupNames {
            groupTOImages[group] = []
            if let people = groupTOPeople[group] {
                var mycount = 0
            for peeps in people {
                InstagramEngine.sharedEngine().getUserDetails(peeps, withSuccess: { user in
                    if let ppic = user.profilePictureURL {
                        let picUrl = ppic.absoluteString
                        print(picUrl)
                        ImageLoader.sharedLoader.imageForUrl(picUrl) { (image, url) -> () in
                            self.groupTOImages[group]?.append(image!)
                            mycount++
                            if mycount == people.count {
                                self.tableView.reloadData()

                            }
                        }
                    } else {
                        self.groupTOImages[group]?.append(UIImage())
                        mycount++
                        if mycount == people.count {
                            self.tableView.reloadData()
                        }
                    }


                    }, failure: nil )
            }
        }

        }

var counter = 0
var groupCount = 0
var groupCounter = 0
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellInfo = GroupCellsArray[indexPath.section]
     ...
     case .userGroups:
            let cell = tableView.dequeueReusableCellWithIdentifier(cellInfo.description, forIndexPath: indexPath) as! GroupTableViewCell
            if groupNames.count > 0 {
                var gp = groupNames[groupCounter]
                switch counter {
                    case 0:
                        cell.firstTitle.text = (gp.valueForKey("name") as! String)
                        if let ourImages = groupTOImages[gp] {
                        for image in ourImages {
                            print(image.description)
                            print("groupCount \(groupCounter)")
                            cell.firstUserButtons[groupCount].layer.borderWidth = 0
                            cell.firstUserButtons[groupCount].setImage(image, forState: .Normal)
                            groupCount++
                            if groupCount == ourImages.count {
                                groupCount = 0
                                counter++
                                groupCounter++
                                gp = groupNames[groupCounter]
                            }
                        }
                    }
                    case 1:
                    if let title = gp.valueForKey("name") as? String {
                        cell.secondTitle.text = title
                        if let ourImages = groupTOImages[gp] {
                            for image in ourImages {
                                cell.secondUserButtons[groupCount].layer.borderWidth = 0
                                cell.secondUserButtons[groupCount].setImage(image, forState: .Normal)
                                groupCount++
                                if groupCount == ourImages.count {
                                    groupCount = 0
                                    counter = 0
                                    groupCounter++
                                    gp = groupNames[groupCounter]
                                }
                            }

                        }
                    } else {
                        cell.secondTitle.text = "Title"
                    }
                default:
                    break
                }
每行如下图所示:

使用CellForRowatineXpath中的ImageLoader进行编码:

var counter = 0
        for group in groupNames {
            print("in the second")
            groupTOImages[group] = []
            if let people = groupTOPeople[group] {
                var mycount = 0
            for peeps in people {
                InstagramEngine.sharedEngine().getUserDetails(peeps, withSuccess: { user in
                    if let ppic = user.profilePictureURL {
                        let picUrl = ppic.absoluteString
                        self.groupTOImages[group]?.append(picUrl)
                        counter++
                        mycount++
                        if counter == self.groupNames.count && mycount == people.count
                        {
                            dispatch_async(dispatch_get_main_queue()) {
                                self.tableView.reloadData()
                            }
                        }
                    } else {
                        self.groupTOImages[group]?.append(nil)
                        counter++
                        mycount++
                        if counter == self.groupNames.count && mycount == people.count
                        {
                            dispatch_async(dispatch_get_main_queue()) {
                                self.tableView.reloadData()
                            }
                        }
                    }

                }, failure: nil )
            }
        }


 if groupNames.count > 0 {
                var gp = groupNames[groupCounter]
                print("counter!!!!")
                print("groupCount \(counter)")
                switch counter {
                    case 0:
                        if let ourImages = groupTOImages[gp] {
                            cell.firstTitle.text = (gp.valueForKey("name") as! String)
                            print(cell.firstTitle.text)
                            for image in ourImages {
                                if let url = image {
                                    print("I get in here")
                                    ImageLoader.sharedLoader.imageForUrl(url) { (image, url) -> () in
                                    cell.firstUserButtons[self.groupCount].layer.borderWidth = 0
                                    cell.firstUserButtons[self.groupCount].setImage(image, forState: .Normal)
                                        self.groupCount++
                                        if self.groupCount == ourImages.count {
                                            self.groupCount = 0
                                            self.counter++
                                            self.groupCounter++
                                            gp = self.groupNames[self.groupCounter]
                                        }
                                    }

                                } else {
                                    self.groupCount++
                                    if self.groupCount == ourImages.count {
                                        self.groupCount = 0
                                        self.counter++
                                        self.groupCounter++
                                        gp = self.groupNames[self.groupCounter]
                                    }
                                }
                            }
                        }
                case 1:
                    if let title = gp.valueForKey("name") as? String {
                        cell.secondTitle.text = title
                        if let ourImages = groupTOImages[gp] {
                            for image in ourImages {
                                if let url = image {
                                    ImageLoader.sharedLoader.imageForUrl(url) { (image, url) -> () in
                                    cell.secondUserButtons[self.groupCount].layer.borderWidth = 0
                                    cell.secondUserButtons[self.groupCount].setImage(image, forState: .Normal)
                                    self.groupCount++
                                        if self.groupCount == ourImages.count {
                                            self.groupCount = 0
                                            self.counter++
                                            self.groupCounter++
                                            gp = self.groupNames[self.groupCounter]
                                        }
                                    }
                                } else {
                                    self.groupCount++
                                    if self.groupCount == ourImages.count {
                                        self.groupCount = 0
                                        self.counter = 0
                                        self.groupCounter++
                                        gp = self.groupNames[self.groupCounter]
                                    }
                                }
                            }

                        }
                    } else {
                        cell.secondTitle.text = "Title"
                    }

您应该将
self.tableView.reloadData()
替换为

dispatch_async(dispatch_get_main_queue(), { () -> Void in 
    self.tableView.reloadData()
})

希望这有帮助

您是否在
dispatch\u async(dispatch\u get\u main\u queue())
中调用了您的
reloadData()
?如果是的话,这可能是因为重用了ImageLoader completion闭包中调用的
tableView
@Tj3n
reloadData()。
dispatch\U async(dispatch\U get\U main\U queue()
将在哪里发挥作用?你必须调用
dispatch\U async(dispatch\U get\U main\U queue()
每当你想处理UI内容时,每当你得到任何与UI相关的东西需要更新时,就调用它,比如
dispatch\U get\U main\U queue(){self.tableView.reloadData()}
@Tj3n好的,但我不确定该放在哪里。在
getGroups()
函数的末尾,在ImageLoader闭包内,等等?是的,只需重新绘制整个
dispatch\u async(dispatch\u get\u main\u queue(){self.tableView.reloadData}
和正常的
self.tableView.reloadData())
anywhere in closure then done:D您可以在另一个闭包中的任何位置写入该代码,以确保UI在主线程中运行