【发布时间】:2015-09-08 06:58:27
【问题描述】:
我在UITableView 中实现了UIWebview。当我滚动表格视图时,数据重新加载并且UIWebview 有一些闪烁。这是因为每当我滚动表格视图时都会重新加载单元格。我可以保存单元格的状态或将UIWebview 的数据保存在NSMutableArray 中以加快加载速度,但我无法保存单元格。那么我怎样才能保存单元格或防止一次又一次地重新加载单元格。我希望这些单元格只重新加载一次。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("videoCell") as! UITableViewCell
// var height = UIScreen.mainScreen().applicationFrame.size.height
let linksCell = links[indexPath.row].videoLink
let winksCell = links[indexPath.row]
mutArray.insertObject(linksCell!, atIndex: indexPath.row)
println(mutArray)
if let webView = cell.viewWithTag(21) as? UIWebView {
webView.scrollView.scrollEnabled = false
webView.scrollView.bounces = false
var html = "<html><body><iframe src=\"http://www.youtube.com/embed/\(mutArray[indexPath.row])\" width=\"\(videoTable.frame.width - 16)\" height=\"200\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
webView.loadHTMLString(html, baseURL: nil)
// println(linksCell.videoLink)
// println(html)
}
if let videoName = cell.viewWithTag(22) as? UILabel {
videoName.text = winksCell.title
videoName.adjustsFontSizeToFitWidth = true
}
return cell
}
【问题讨论】:
-
虽然不推荐,但是不使用
dequeueReusableCellWithIdentifier也可以达到这个目的 -
你在某处调用 reloadData 吗?如果不是,正如 Nishant 所说,删除 dequeCell。
-
dequeueReusableCellWithIdentifier 的替代方案是什么?
-
Alex 我在 viewDidLoad 中添加了 reloadData,我需要这样做来显示 tableview 的数据。如何在没有 dequeCell 的情况下调用单元格?
-
您必须创建单元格数组(例如在 viewDidLoad 中),然后在 cellForRowAtIndexPath 中从数组中获取正确的单元格。
标签: ios objective-c swift uitableview webview