Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中,有没有一种方法可以直接设置现有UITableViewCell';谁是鉴定人?_Swift_Uitableview - Fatal编程技术网

在Swift中,有没有一种方法可以直接设置现有UITableViewCell';谁是鉴定人?

在Swift中,有没有一种方法可以直接设置现有UITableViewCell';谁是鉴定人?,swift,uitableview,Swift,Uitableview,我从主单元格复制新单元格,如下所示: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell : UITableViewCell? let cellId = String(format: "Cell%d", indexPath.row) cell = alertTable!.dequeueReu

我从主单元格复制新单元格,如下所示:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell : UITableViewCell?
    let cellId = String(format: "Cell%d", indexPath.row)
    cell = alertTable!.dequeueReusableCellWithIdentifier(cellId) as! UITableViewCell?
    if cell == nil {
        let archivedData = NSKeyedArchiver.archivedDataWithRootObject(masterTableCell!)
        cell = NSKeyedUnarchiver.unarchiveObjectWithData(archivedData) as! UITableViewCell?
        cell!.reuseIdentifier = cellId   // compiler error: cannot assign to the result of this expression
    }
    populateAlertTableCell(cell!, alertIdx: indexPath.row)
    return cell!
}

但如果你仔细观察,你就会发现问题所在。我从不重用任何单元格,因为我没有设置重用标识符。事实上,我真正需要的是一种找到单元格行的方法,我已经在用标签做其他事情了。因此,如果reuseIdentifier起作用,那么它可以启用单元格重用,并且也是我识别单元格行的方式。

为了实现每个索引路径的单元格重用,
UITableView
提供了
func-dequeueReusableCellWithIdentifier(\uIdentifier:String,forindexath-indexath:nsindexath)->UITableViewCell
。这将返回与之前占用给定索引路径的单元格实例相同的单元格实例,如果这样做是有效的。没必要再猜了

正常用法是以前使用的
func registerNib(nib:UINib?,

forCellReuseIdentifier标识符:String)
,或者直接在故事板或NIB中设计单元,并让表视图根据需要创建单元类的实例。

我不确定是否正确理解了您的问题

首先,您需要注册一个单元格,如下所示:

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
这应该在设置UITableView时完成

在那之后

tableView.dequeueReusableCellWithIdentifier("cell")
应该会给你想要的结果

现在转到您的特定代码:

如果调用了
cellforrowatinexpath
,则不应像创建单元格那样创建单元格,因为这样就消除了使用
dequeueReusableCellWithIdentifier
的全部意义。最后提到的方法要么为您传递的标识符提供一个可重用的单元格,要么为您提供一个新的单元格。这意味着您不应该自己创建单元格

为了更好地理解,以下是您在代码(简化)中的基本操作方式:


很抱歉,为什么要将行号插入重用ID?好的,当您创建单元格时,它应该具有唯一的重用ID。行号对于单元格来说是一个很好的唯一编号。我还需要从UITableViewCell对象中获取每个单元格的行号。如果我可以这样设置的话,我也可以使用重用ID。你看到这个策略的弱点了吗?不,重用ID应该是一类单元所独有的。通常它们是特定UITableViewCell子类的同义词。重用ID的主要用途是在创建和删除视图时保存。ios6中引入的
forIndexPath:
机制和通过重用ID进行NIB/类注册的内容处理何时需要通过重用集合中的单元所处的位置来识别它们。
var tableView: UITableView?

override func loadView() {
    super.loadView()

    self.tableView = UITableView(frame: CGRectZero)
    // instead of UITableViewCell you could also register you own cell (with another identifier)
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.view.addSubview(self.tableView)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    self.tableView.frame = self.view.bounds
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITablViewCell

    // set properties of you cell here
    // if you don't change the properties of your cell, it will still be reused,
    // meaning that a cell that just disappeard at the bottom will appear at the top unchanged        

    return cell
}