JFe*_*d-9 1 uitableview ios swift
我意识到有几个与此类似的问题,我确实检查了很多。但是,我检查的那些要么使用错误的语言,要么不是我想要的,因此我提出了一个新问题。作为参考,我在 Swift 2 中使用 XCode 7.3.1。
我的新 iOS 应用程序的一项功能是在互联网上搜索数据,并通过填充 UITableView 来显示结果。我的问题是它在每个单元格中显示图片,这使得滚动非常烦人。
我理想的解决方案是有一个默认的图片显示,在允许用户继续滚动的同时,它会在后台下载。然后,一旦它检索到它,它将替换默认值。我看到了一些关于如何做到这一点的先前答案,但没有一个对我有用。
PS如果你看到另一个用SWIFT解决这个问题的问题,发布网址,我会删除这个。
这是我目前使用的代码(有延迟):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // Creates each cell, by parsing through the data received from the Employees array which we returned from the database
if (Employees.count == 0)
{
let cell = self.ResultsTable.dequeueReusableCellWithIdentifier("NORESULT", forIndexPath: indexPath) as! TruckSearchNRTableViewCell
return cell
}
else {
let cell = self.ResultsTable.dequeueReusableCellWithIdentifier("RESULT", forIndexPath: indexPath) as! TruckSearchTableViewCell
cell.companyLabel.text = Employees[indexPath.row]["CompanyName"] as? String
cell.nameLabel.text = Employees[indexPath.row]["EmployeeName"] as? String
if let mobile = Employees[indexPath.row]["PhoneNumber"] as? String {
if (mobile == "")
{
cell.mobileLabel.hidden = true
cell.mobileTitle.hidden = true
} else {
cell.mobileTitle.hidden = false
cell.mobileLabel.hidden = false
}
let phonenumber = mobile.stringByReplacingOccurrencesOfString("[^0-9]", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range:nil);
cell.phoneNumber.text = phonenumber
}
cell.titleLabel.text = Employees[indexPath.row]["JobTitle"] as? String
cell.truckLabel.text = Employees[indexPath.row]["TruckNumber"] as? String
cell.supervisorLabel.text = Employees[indexPath.row]["SupervisorName"] as? String
if (Employees[indexPath.row]["Synced"] as? Int) == 0 {
turnRed(cell)
} else {
turnBlack(cell)
}
if let ePhoto = Employees[indexPath.row]["PicLocation"] as? String { // Save complete URL of picture location, and save it to the table
let url = NSURL(string: "https://exampleurl.com/\(ePhoto)")!
if let data = NSData(contentsOfURL: url){
let myImage = UIImage(data: data)
cell.employeePhoto.image = myImage
}
else
{
cell.employeePhoto.image = UIImage(named: "person-generic")
}
}
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
问题是您正在主线程上检索图片,这阻塞了它。所有 UI 操作,包括滚动,都是在主线程上执行的,这就是它目前不稳定的原因。
为了解决这个问题,而不是:
if let data = NSData(contentsOfURL: url){
let myImage = UIImage(data: data)
cell.employeePhoto.image = myImage
} else {
cell.employeePhoto.image = UIImage(named: "person-generic")
}
Run Code Online (Sandbox Code Playgroud)
将图像请求放在后台线程上,然后再次在主线程上设置图像。
例如
let qos = QOS_CLASS_USER_INITIATED
dispatch_async(dispatch_get_global_queue(qos, 0), {
if let data = NSData(contentsOfURL: url){
let myImage = UIImage(data: data)
dispatch_async(dispatch_get_main_queue(), {
cell.employeePhoto.image = myImage
})
} else {
dispatch_async(dispatch_get_main_queue(), {
cell.employeePhoto.image = UIImage(named: "person-generic")
})
}
})
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用异步 url 会话来获取数据,如下所示:
let request: NSURLRequest = NSURLRequest(URL: url)
let mainQueue = NSOperationQueue.mainQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in
if error == nil {
let myImage = UIImage(data: data)
dispatch_async(dispatch_get_main_queue(), {
cell.employeePhoto.image = myImage
})
} else {
dispatch_async(dispatch_get_main_queue(), {
cell.employeePhoto.image = UIImage(named: "person-generic")
})
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3033 次 |
| 最近记录: |