Ios Swift在TableView上重新加载数据
当我从Facebook的SDK/API接收数据时,我正在重新加载TableView。我想用这些数据填充我的tableView,但当我调用Ios Swift在TableView上重新加载数据,ios,facebook,swift,uitableview,Ios,Facebook,Swift,Uitableview,当我从Facebook的SDK/API接收数据时,我正在重新加载TableView。我想用这些数据填充我的tableView,但当我调用self.friendsTable.reloadData()时,它确实很慢,甚至当我将它添加到dispatch\u async()时也是如此 这是myFacebookController中的getFriends()函数 func getFriends(){ let friendsReq = FBSDKGraphRequest(graphPath:
self.friendsTable.reloadData()
时,它确实很慢,甚至当我将它添加到dispatch\u async()
时也是如此
这是myFacebookController
中的getFriends()
函数
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
})
}
}
}
下面是cellforrowatinexpath()
函数:
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
}
cellforrowatinedexpath
中的dispatch\u async
确实是不必要的-这可能是您的问题的根源
该方法在填充表时在内部调用,因此可以理所当然地在主线程中执行该方法
相反,在您的实现中,单元被创建或出列、返回,并在稍后进行配置,因为传递给dispatch\u async
的闭包是在流外执行的
只要去掉
cellForRowAtIndexPath
中的dispatch\u async
,它就应该可以工作。将数据加载到cellForRow上的UIView中没有任何意义,因为它位于dispatch Block中。您可以发布所有其他uitabiewDatasource
和UITableViewDelegate
方法实现吗?可能是其中一个帖子中的某个内容发布在numberofrowsin部分
too@jbehrens94:在您的单元格配置中,尝试仅保留cell.friendName.text=self.friendsNames[indexath.row]
,对下一行和前4行进行注释,这确实很有帮助!它又快又快!我确实删除了它,但它仍然非常慢,tableView确实需要几秒钟才能滚动。滚动时慢(意味着表格移动需要很长时间)还是正常滚动,但更新每个单元格中的数据时慢?滚动时慢,表格移动需要很长时间。@Woodstock:不确定,但是我认为在内部,startWithCompletionHandler
不会在主线程上运行,所以它可能不会运行needed@jbehrens94:这可能表明问题出在其他地方-例如,主线程上的某个东西正在消耗CPU。查看getFriends()
调用后执行的代码,或者做一些分析来找出原因