首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tableView.dequeueReusableCellWithIdentifier对tableView.cellForRowAtIndexPath

tableView.dequeueReusableCellWithIdentifier对tableView.cellForRowAtIndexPath
EN

Stack Overflow用户
提问于 2015-11-06 05:12:33
回答 2查看 3.7K关注 0票数 4

我的Xcode是7.1。模拟器是iPhone6。

通常我使用dequeueResuableCellWithIdentifier,但这一次我遇到了问题。

我的问题是:

在选择新行后,它不会清除我的orangeColor()。但是取消选择indexPath的打印行是正确的。

我的解决方案是:

我变了

let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell;

let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell;

然后它解决了我的问题。背景色是clearColor()

这里是我的文件:

MyCell.swift:

代码语言:javascript
复制
import UIKit

class MyCell: UITableViewCell {
    @IBOutlet weak var myLabel : UILabel!
    @IBOutlet weak var myWebView : UIWebView!;
    //@IBOutlet weak var myView : UIView!;
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code.
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

ViewController.swift :

代码语言:javascript
复制
import UIKit

class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableViewObject: UITableView!

    var fruits = ["Apple", "Banana","Cherry", "Durian", "ElderBerry"];
    var selectedIndexPath : NSIndexPath?;
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableViewObject.delegate = self;
        self.tableViewObject.dataSource = self;
        // Do any additional setup after loading the view, typically from a nib.
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return fruits.count;
    }
    func loadCellContent(acell : UITableViewCell, indexPath : NSIndexPath, color: UIColor){
        let cell = acell as! MyCell;
        cell.myLabel.text = String(UnicodeScalar(65 + indexPath.row));
        cell.myWebView.loadHTMLString(fruits[indexPath.row], baseURL: nil);
        cell.myWebView.scrollView.bounces = false;
        cell.myWebView.scrollView.scrollEnabled = false;
        cell.myWebView.userInteractionEnabled = false;
        cell.backgroundColor = color;
        cell.myLabel.backgroundColor = color;
        cell.myWebView.backgroundColor = color;
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell;
        self.loadCellContent(cell, indexPath: indexPath, color: UIColor.clearColor());
        return cell;
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int{
        return 1;
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell;
        let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell;
        self.loadCellContent(cell, indexPath: indexPath, color : UIColor.orangeColor());
    }
    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        //let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell;
        let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell;
        self.loadCellContent(cell, indexPath: indexPath,color: UIColor.clearColor());
        print("Hellos", indexPath.row);

    }
}

我的问题是:

  1. 方法dequeueReusableCellWithIdentifercellForRowAtIndexPath的主要区别是什么?
  2. 什么时候使用第一个或第二个,我是如何知道的?这一次,我使用从一个教程到另一个教程的试用/错误。

更新:

我的背景是物理和一点计算机科学。请不要因为我天真的问题而生气。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-06 07:38:11

tableview试图在内存中的任何时候都没有所有索引路径的单元对象实例:

  • 'dequeueReusableCellWithIdentifer‘回收或创建一个单元格,应该在’cellForRow.‘中调用它。这个总是返回一个单元格实例,或者是一个新的,或者是以前不可见的。在那之后,你准备好细胞以供展示。
  • 'cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell? // 返回零,如果单元格不可见或索引路径超出范围‘--这不会创建单元格,只会让您访问它们。我认为应该尽可能避免意外泄漏和其他对tableView的干扰。

关于你的“setSelected”问题:

  • 在单元格类中实现准备重用(将所有值设置为默认值)是非常好的。
  • 您不需要在'didSelectRow‘中再次加载单元内容(用户刚才点击了它,所以它被加载了)
  • 您可能需要在单元格的“setSelected”中的橙色/透明颜色之间进行更改
票数 3
EN

Stack Overflow用户

发布于 2015-11-06 18:20:00

这两种方法用途不同。

  • 如果有必要,dequeueReusableCellWithIdentifier:indexPath:将实例化一个新的单元格,但将重用以前在屏幕上滚动过的单元格(如果可用的话)。 您只从tableView:cellForRowAtIndexPath:调用此方法。
  • cellForRowAtIndexPath:方法(不要与同名的tableView:cellForRowAtIndexPath:方法混淆)不会实例化一个新的单元格,而是只用于获取对当前可见的以前实例化单元格的引用。如果单元格不可见(例如,在屏幕外滚动),则返回nil。 在tableView:didSelectRowAtIndexPath:tableView:didDeselectRowAtIndexPath:等方法中使用此方法,因为您只是试图获取对现有单元格的引用,而不是实例化新的单元格。

--

注意,在代码示例中,您不仅从loadCellContent调用tableView:cellForRowAtIndexPath:,而且也从tableView:didSelectRowAtIndexPath:tableView:didDeselectRowAtIndexPath:调用。我建议将单元格的初始配置(例如,只从tableView:cellForRowAtIndexPath:调用的loadCellContent )与背景颜色的调整分离开来(您将在didSelect...didDeselect...中这样做)。后两种方法通常不会重新配置整个单元格,而是只更新背景颜色(或执行任何适当的装饰以显示所选内容)。

此外,您还需要确保tableView:cellForRowAtIndexPath:检查在配置背景色时是否选中了单元格。用户可能会选择一个单元格,将其滚动到屏幕上,然后在屏幕上回滚,在这种情况下,tableView:cellForRowAtIndexPath:必须检查单元格是否被选中以设置背景色。

例如:

代码语言:javascript
复制
// MARK: Cell configuration

func loadContentForCell(cell: UITableViewCell, indexPath: NSIndexPath) {
    // do whatever you need here; I'm just going to set the `textLabel`
    cell.textLabel?.text = "Row \(indexPath.row)"
}

func updateBackgroundColorForCell(cell: UITableViewCell?) {
    cell?.contentView.backgroundColor = cell?.selected == true ? UIColor.redColor() : UIColor.whiteColor()
}

// MARK: UITableViewDataSource

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 100
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    
    loadContentForCell(cell, indexPath: indexPath)
    updateBackgroundColorForCell(cell)
    
    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    updateBackgroundColorForCell(cell)
}

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    updateBackgroundColorForCell(cell)
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33559932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档