如何在UITableView中重用单元格?

我今天在我的应用程序中遇到了一些问题。 我正在使用UITableView呈现不同的单元格,其中一些包含特定按钮,一些显示您当前所在的游戏,一些仅用于单元格之间的间距。

但是,当我执行tableView.reloadData()时,我遇到了一些问题。

当我点击“新游戏”时,应该添加一个新的游戏单元格,但不是最后一个单元格向下移动(如预期的那样,因为介于两者之间),但是应该更改的上层单元格却不会。 我希望这是因为重用(或“ 缓存 ”)。 也许有人可以帮我解决这个问题。

这是正在发生的 解释 的图像

现在我有不同的单元格,所有单元格都有自己的标识符,例如:“Emptycell”,“Gamecell”,“startnewgameBtnCell”。 我这样做是因为我在storybuilder中创建了每个单元格。

这是我的cellForRowAtIndexPath代码

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = UITableViewCell() let identifier = cell_identifiers[indexPath.row][0] as! String if ((cell_identifiers[indexPath.row][1] as! String) == "MYTURN" ) { var cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! GameViewCell let match = self.myTurnMatches[indexPath.row - 4] setupGameViewCellObject(match) } if ((cell_identifiers[indexPath.row][1] as! String) == "NOT" ) { var cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! FirstMenuTableViewCell } return cell 

在这段代码中,我检查它是否是一个游戏单元,空单元或包含标签或按钮的单元格都是FirstMenuTableViewCell。 只有GameCell拥有自己的GameViewCell类。

我还仔细检查了我的标识符是否正确构建并且它们是。

也许有人可以解释我到底发生了什么,以及解决我所处情况的正确方法,我想我可能还不完全了解如何将UITableViewCell与自定义单元格一起使用。

谢谢阅读

您可以在if语句中创建名为cell的新变量。 你正在隐藏外部单元格。

使用一个单元格变量。

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell: UITableViewCell let identifier = cell_identifiers[indexPath.row][0] as! String if ((cell_identifiers[indexPath.row][1] as! String) == "MYTURN" ) { cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! GameViewCell let match = self.myTurnMatches[indexPath.row - 4] setupGameViewCellObject(match) } if ((cell_identifiers[indexPath.row][1] as! String) == "NOT" ) { cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! FirstMenuTableViewCell } return cell } 

尝试更新您的代码:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = UITableViewCell() let identifier = cell_identifiers[indexPath.row][0] as! String if ((cell_identifiers[indexPath.row][1] as! String) == "MYTURN" ) { var cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! GameViewCell let match = self.myTurnMatches[indexPath.row - 4] setupGameViewCellObject(match) return cell } if ((cell_identifiers[indexPath.row][1] as! String) == "NOT" ) { var cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! FirstMenuTableViewCell return cell } return cell 

如果我,我编码:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let identifier = cell_identifiers[indexPath.row][0] as! String if ((cell_identifiers[indexPath.row][1] as! String) == "MYTURN" ) { let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! GameViewCell let match = self.myTurnMatches[indexPath.row - 4] setupGameViewCellObject(match) return cell }else { // if you sure just have 2 cell type let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! FirstMenuTableViewCell return cell } } 

但我认为你应该在tableview中使用section,它更好。 所以,你有3个部分和2个headerofsection。 第一部分有1行,第二部分有self.myTurnMatches.count行….:

  override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { if section == 0 || section == 1{ return 44 }else{ return 0 } } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.section == 1{ let cell = tableView.dequeueReusableCellWithIdentifier("GameViewCell 's identifier", forIndexPath: indexPath) as! GameViewCell let match = self.myTurnMatches[indexPath.row - 4] setupGameViewCellObject(match) return cell }else{ let cell = tableView.dequeueReusableCellWithIdentifier("FirstMenuTableViewCell 's identifier", forIndexPath: indexPath) as! FirstMenuTableViewCell return cell } }