Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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_Cell_Subclass - Fatal编程技术网

Swift 是否可以将自定义UITableViewCell存储到数组中?

Swift 是否可以将自定义UITableViewCell存储到数组中?,swift,uitableview,cell,subclass,Swift,Uitableview,Cell,Subclass,我有很多自定义单元格,希望通过使用结构并将有关单元格的信息存储在数组中来简化cellForRowAt struct HowToCells { let helpCell: UITableViewCell let identifier: String let icon: UIImage let cellName: String } var cellArray = [HowToCells]() cellArray.append(HowToCells.init(hel

我有很多自定义单元格,希望通过使用结构并将有关单元格的信息存储在数组中来简化cellForRowAt

struct HowToCells {
    let helpCell: UITableViewCell
    let identifier: String
    let icon: UIImage
    let cellName: String
}

var cellArray = [HowToCells]()

cellArray.append(HowToCells.init(helpCell: ScreenRecordingTableViewCell(), identifier: "ScreenRecordingTableViewCell", icon: HowToImage.screenRecording.image(), cellName: "Enable screen recording"))

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

     let c = cellArray[indexPath.row]
     let cellToUse = c.helpCell

     let cell = tableView.dequeueReusableCell(withIdentifier: c.identifier, for: indexPath) as! cellToUse

}
我得到以下错误:使用未声明的类型“cellToUse”


ScreenRecordingTableViewCell是一个自定义UITableViewCell

您在这一行得到错误: 让cell=tableView.dequeueReusableCellwithIdentifier:c.identifier,for:indexPath as!细胞的

由于UITableViewCell子类类型应在as!之后使用!,不是一个变量名。x as!y要求强制将对象X的类型转换为类型y


也就是说,您将遇到在数组中存储单元格和使用tableView.dequeueReusableCell时出现的问题,它将获取现有单元格,将其出列,然后重新使用它。这将损坏您的数据。您需要将单元格的所有数据放入数组中,然后从cellForRow中的数组中检索该索引

您不需要在数组中存储表视图单元格,因为UITableView自己创建并存储它们。只需在tableView中使用tableView.dequeueReusableCellwithIdentifier:for:方法来获取所需的单元格。之后,您可以配置此单元格

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CellClass
// Configure cell
return cell
}

这正是引入可重用单元格的原因:为了使不可见的单元格不在内存中……希望通过使用结构并在数组中存储有关单元格的信息来简化cellForRowAt。然后将信息存储在数组中,而不是存储单元本身,顺便说一句,为了使表正常工作,您必须这样做@RakeshaShastri-ScreenRecordingTableViewCell是本例中存储在阵列中的实际单元格。苹果是否建议我们不存储UITableViewCells?@ReDetection。是的,单元格是可删除的,这意味着iOS管理其生命周期,应用程序应避免使用uitableview数据源和委托方法与之交互。重点管理视图层单元格显示的数据,而不是单元格本身。我的意思是,您是否有指向developer.apple.com的链接,其中明确指出应避免与单元格混淆?我相信,如果我不使用dequeueReusableCell方法,而是提前创建数量非常有限的单元,在创建后不改变,我可以安全地将它们存储在数组中。