【问题标题】:ios-Prevent reloading of cell in UITableView on scrollingios-防止UITableView滚动时重新加载单元格
【发布时间】: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


【解决方案1】:

试试这个:我没有测试它。但试一试:

替换:

 let cell = tableView.dequeueReusableCellWithIdentifier("videoCell") as! UITableViewCell

let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "videoCell")

【讨论】:

  • 感谢 iAnurag 的帮助,但这件事没有用。它显示一个白色的屏幕。我使用调试器,发现单元格已加载但其内容未加载。
【解决方案2】:

如果您正在使用情节提要,则创建一个自定义单元格类 并在

中获取带有标识符的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
YourCustomeCell_Class *cell = (YourCustomeCell_Class *)[tableView dequeueReusableCellWithIdentifier:@"MyCellIndentifier"]; 
return cell; 
}

试试看。希望它能解决你的问题。

【讨论】:

  • 他不想让单元格去队列。
  • 这行不通。我不想让细胞出队。我想将单元格保存在内存中。
  • 您可以添加临时检查。每次在 cellForRowAtIndexPath 上的 MutableArray 中添加 indexPath.row 的值,并将 mutableArrayValue 与相同的单元格匹配。如果存在,则不要像您当前编写的那样在 cellForRowAtIndexPath 中调用您的代码。
  • @ekant 我也试过了,但是在这个已经加载的单元格中加载了与新加载的单元格相同的单元格内容。
猜你喜欢
  • 1970-01-01
  • 2018-06-16
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多