我的表設置完美,當我滾動它,它調用cellForRowAtIndexPath
加載實際行,它有滯後的第二秒凍結,並導致表不流暢。我做了一些研究,爲什麼會這樣,就我的理解而言,這是因爲我沒有重複使用單元格。我很快就編程,而且我也很新。我想知道是否有人可以幫助修復我的Swift代碼重用單元格,這樣當我滾動表格時,它的流暢和滯後自由。UITableView cellForRowAtIndexPath問題與使用重用標識符
這裏是我的代碼:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
mainTableView.estimatedRowHeight = 50.0
mainTableView.rowHeight = UITableViewAutomaticDimension
var cell : postWithTitleAndImageTableViewCell = mainTableView.dequeueReusableCellWithIdentifier("postWithTitleAndImage") as postWithTitleAndImageTableViewCell
var imageID = json[indexPath.row]["imageName"] as String
var id = json[indexPath.row]["id"] as String
var isImage = json[indexPath.row]["isImage"] as String
var title = json[indexPath.row]["name"] as String
var text = json[indexPath.row]["post"] as String
var time = json[indexPath.row]["time"] as String
var popularity = json[indexPath.row]["popularity"] as String
//add clientid
//time handle in the cell class
lastID = id
cell.setCell(id, title: title, imageID: imageID, text: text, time: time, popularity: popularity)
return cell as postWithTitleAndImageTableViewCell
}
如果我需要提供任何詳細資訊,請讓我知道。我修正這個問題非常重要。
編輯: 我發現性能問題在我的代碼中。在我的自定義單元類中有一個setCell函數來設置單元格中的所有內容。在這段代碼中,我有一個web請求來獲取圖像。這是一個必要的調用,所以我想解決性能問題。
這裏是setCell:
public func setCell(id: String, title: String, imageID: String, text: String, time: String, popularity: String){
println("setCell called")
readIDs.append(id)
postID = id
let colors = ["person-blue.png", "person-green.png", "person-orange.png", "person-pink.png", "person-purple.png", "person-red.png", "person-yellow.png"]
let randomIndex = Int(arc4random_uniform(UInt32(colors.count)))
var imageName = (colors[randomIndex])
var cellHeight = TableViewCell.frame.height
let font = UIFont(name: "Arial Rounded MT Bold", size: 14.0)
labelHeightConstraint.constant = heightForView(text, font: font!, width: postText.frame.width)
postImage.clipsToBounds = true
var timeDouble = time as NSString
var phpTime = timeDouble.doubleValue
var currentDate = NSDate()
var postDate = NSDate(timeIntervalSince1970: phpTime as NSTimeInterval)
var postTimeOffset = currentDate.offsetFrom(postDate) as String
/*let url = NSURL(string: "http://bangedabigailtwice.tk/kno/getImage.php?id=\(imageID)")
var err: NSError?
var imageData :NSData = NSData(contentsOfURL: url!,options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)!
var imageFromURL = UIImage(data:imageData)
*/
self.personImage?.image = UIImage(named: imageName)
self.postTitle?.text = title
self.postText?.text = text
//self.postImage?.image = imageFromURL
self.postImage.userInteractionEnabled = true
self.postTime?.text = postTimeOffset
self.popularityOutlet?.text = popularity
let recognizer = UITapGestureRecognizer(target: self, action:Selector("tapImageView:"))
recognizer.numberOfTapsRequired = 2
postImage.addGestureRecognizer(recognizer);
}
如果你看一下上面的代碼,我已經註釋掉導致性能問題的線路。現在對於我的問題,我如何臨時存儲圖像供以後使用,以及如何在臨時存儲的圖像存儲後轉儲臨時存儲的圖像?
您可能會考慮在後臺任務中預取圖像。請參閱GDC例程(Grand Central Dispatch)。 – zisoft 2014-12-08 08:11:23