简体   繁体   中英

Swift reloadData on TableView

I am reloading my TableView when I receive data from Facebook's SDK/API. I want to populate my tableView with that data, but when I call self.friendsTable.reloadData() it is really laggy and slow, even when I added it to dispatch_async() .

This is the getFriends() function in my FacebookController .

func getFriends(){
        let friendsReq = FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: nil)
        friendsReq.startWithCompletionHandler{
            (connection, result, error) -> Void in
            if error != nil{
                let alertCtrl = UIAlertController(title: "Geen vrienden?", message: "We konden geen vrienden vinden..", preferredStyle: .ActionSheet)
                let confirmAction = UIAlertAction(title: "Begrepen!", style: .Cancel, handler: { (action) -> Void in
                    // Do something?
                })
                alertCtrl.addAction(confirmAction)
                self.presentViewController(alertCtrl, animated: true, completion: nil)
            } else {
                let data = result["data"]
                for friend in data as! NSArray{
                    self.friendsNames.append(friend["name"] as! String)
                    self.friendsImages.append((friend["picture"]!!["data"]!!["url"] as? String)!)
                }

                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.tableView(self.friendsTable, numberOfRowsInSection: self.friendsNames.count)
                    self.friendsTable.reloadData()
                    return
                })

            }

        }
    }

And here is the cellForRowAtIndexPath() function:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("FriendCell") as! FriendTableViewCell

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            let img = self.friendsImages[indexPath.row]
            let imgUrl = NSURL(string: img)
            let data = NSData(contentsOfURL: imgUrl!)
            let image = UIImage(data: data!)
            cell.friendName.text = self.friendsNames[indexPath.row]
            cell.friendImage.image = image
        })

        return cell
    }

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Do something
        return self.friendsNames.count
    }

The dispatch_async in cellForRowAtIndexPath is really unneeded - and probably that's the source of your problems.

That method is invoked internally when populating the table, so it can be given for granted that it is executed in the main thread.

Instead in your implementation the cell is created or dequeued, returned, and at a later time it's configured, because the closure passed to dispatch_async is executed out of the flow.

Just get rid of dispatch_async in cellForRowAtIndexPath , and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM