Ios 如何在UITableView中重用单元格?
我今天在应用程序中遇到了一些问题。我使用UITableView显示不同的单元格,有些包含特定的按钮,有些显示您当前所在的游戏,有些仅用于单元格之间的间距 但是,在执行tableView.reloadData()时,我遇到了一些问题 当我点击“新建游戏”时,应该添加一个新的游戏单元格,但不是将最后一个单元格向下移动(就像预期的那样,因为有东西在中间),但是上面的单元格应该改变,它们不会改变。我认为这是因为重用(或“缓存””)。也许有人能帮我解决这个问题 这是正在发生的事情的图像 现在我有了不同的单元格,它们都有自己的标识符,例如:“Emptycell”、“Gamecell”、“StartNewGamebtCell”。我这样做是因为我在storybuilder中创建了每个单元格 这是我的cellforrowatinexpath的代码Ios 如何在UITableView中重用单元格?,ios,swift,uitableview,Ios,Swift,Uitableview,我今天在应用程序中遇到了一些问题。我使用UITableView显示不同的单元格,有些包含特定的按钮,有些显示您当前所在的游戏,有些仅用于单元格之间的间距 但是,在执行tableView.reloadData()时,我遇到了一些问题 当我点击“新建游戏”时,应该添加一个新的游戏单元格,但不是将最后一个单元格向下移动(就像预期的那样,因为有东西在中间),但是上面的单元格应该改变,它们不会改变。我认为这是因为重用(或“缓存””)。也许有人能帮我解决这个问题 这是正在发生的事情的图像 现在我有了不同的单
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
在这段代码中,我检查它是否是gamecell,空单元格或包含标签或按钮的单元格都是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
}
在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中的节,这样更好。所以,你有3个剖面,2个头部剖面。第一部分有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
}
}
尝试更新您的代码:
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中的节,这样更好。所以,你有3个剖面,2个头部剖面。第一部分有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
}
}
你好,谢谢你的回复,我的代码有什么不同?我看不出来。你有3个变量名,指向不同的对象。if语句中的单元格隐藏了您返回的包含空单元格的外部单元格。我的代码使用了一个变量。哇,这就解决了它,谢谢。但我总是回正确的手机,对吗?这是怎么解决的呢?不,u没有返回正确的单元格,因为这些单元格只存在于i语句的范围内。您创建并返回了另一个单元格对象。你永远不应该自己创建一个单元格,而是把它排出来。谢谢你,我刚刚花了8个小时来尝试这一切。你救了我一夜,呵呵!非常感谢。嗨,谢谢你的回复,我的代码有什么不同?我看不出来。你有3个变量名,指向不同的对象。if语句中的单元格隐藏了您返回的包含空单元格的外部单元格。我的代码使用了一个变量。哇,这就解决了它,谢谢。但我总是回正确的手机,对吗?这是怎么解决的呢?不,u没有返回正确的单元格,因为这些单元格只存在于i语句的范围内。您创建并返回了另一个单元格对象。你永远不应该自己创建一个单元格,而是把它排出来。谢谢你,我刚刚花了8个小时来尝试这一切。你救了我一夜,呵呵!非常感谢。以上答案就是解决方案。不过,我会考虑使用部分,谢谢您的时间。以上答案是解决方案。不过,我会考虑使用部分,谢谢你的时间。