我已经阅读了很多,所以除非我的搜索非常糟糕,否则不要将其重复。
基本上,我有一个小表(5个固定行),但是在一个元组中确实有与之关联的索引(这就是为什么我使用动态方式而不是静态方式的原因。
玩家的目标是交换周围的细胞。我不是用笔尖来填充这些细胞,这就是方法。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellID = "Cell"
let customCell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! DefinitionMatchTableViewCell
customCell.background.layer.borderWidth = CGFloat(2.0)
if tableView == nameTableLeft{
customCell.textToDislay!.text = nameList[indexPath.row].0
customCell.background.layer.borderColor = UIColor(red: 255/255, green: 192/255, blue: 1/255, alpha: 1.0).cgColor
let cellIDFromTuple = nameList[indexPath.row].1
customCell.tag = cellIDFromTuple
}
if tableView == definitionTableRight{
customCell.textToDislay!.text = definitionList[indexPath.row].0
customCell.background.layer.borderColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.2).cgColor
let cellIDFromTupleForDefinition = definitionList[indexPath.row].1
customCell.tag = cellIDFromTupleForDefinition
}
customCell.background.backgroundColor = UIColor.clear
customCell.backgroundColor = UIColor.clear
return customCell
}
现在的问题是,在iPhone SE上,这5个单元格无法容纳,导致顶部或底部单元格在检查值是否匹配时丢失了其参考ID。这使游戏无法完成,我需要一种方法,以便可以用与每个字符串关联的索引值填充表,但是表不会重复使用单元格,而是保留在内存中。
我能想到的唯一方法是用数据写入静态表,然后为每个单元格提供一个不可见的索引,该索引与字符串相关联。但是我宁愿动态地做。
任何有关保持这些电池状态的建议将不胜感激。谢谢!
编辑1
这是我根据表格中的索引值检查答案的方法。我认为问题来自.indexPathsForVisibleRows,因为其中一些不可见
for index in nameTableLeft.indexPathsForVisibleRows!{
let leftCell = nameTableLeft.cellForRow(at: index) as? DefinitionMatchTableViewCell
let rightCell = definitionTableRight.cellForRow(at: index) as? DefinitionMatchTableViewCell
if nameTableLeft.cellForRow(at: index)?.tag == definitionTableRight.cellForRow(at: index)?.tag{
leftCell?.background.layer.borderColor = UIColor.green.cgColor
rightCell?.background.layer.borderColor = UIColor.green.cgColor
counter += 1
}else{
leftCell?.background.layer.borderColor = UIColor.red.cgColor
rightCell?.background.layer.borderColor = UIColor.red.cgColor
}
}
如您所见,然后检查标记上的值是否匹配。但是,如果我使用其他方法,我将在这个概念上苦苦挣扎。
我的模特
var nameList : [(String, Int)] = [("Trojan", 0),
("Virus", 1),
("Spyware", 2),
("Ransomware", 3),
("Leakware", 4)]
var definitionList : [(String, Int)] = [("Attaches onto another program. Replicates itself. Something loaded onto your device without your knowledge. Continues to spread", 0),
("When malware is disguised as something else. Usually an e-mail attachment", 1),
("Tracks a user's actions, this includes login details, internet habits and any secure accounts", 2),
("Locks a device or encrypts files and demands money to unlock it", 3),
("Threatens to distribute your information", 4)]
编辑2
由于对答案的帮助,我决定将其添加到我的答案检查中。
for answerIndex in 0...4{
if definitionList[answerIndex].1 == nameList[answerIndex].1{
print("MATCH")
counter += 1
}else{
print("NO MATCH")
}
}
我混淆了定义和名称列表。然后,我决定根据表的总长度查看放置在元组中的两个索引。不幸的是,如果您无法遍历元组,那么我不得不求助于幻数。
因此,这个小循环决定了索引是否在我的元组中对齐,如果索引正确,否则就不正确。再次感谢大家的帮助。
最佳答案
我也解决了类似的问题。我的建议是继续重复使用细胞。将与每个单元格相对应的数据外部存储在单独的模型类中。然后将其存储在可变数组中。当您对单元格进行混洗时,请对可变数组中的数据进行混洗。
可以通过以下方式完成。model
类
class model: NSObject {
var name: String = ""
var definition: String = ""
var type: String = ""
required init(name: String, definition: String, type: String) {
super.init()
self.name = name
self.definition = definition
self.type = type
}
}
自定义
UITableViewCell
-不同布局的简单示例class CustomTableViewCell: UITableViewCell {
var nameLabel: UILabel = UILabel()
var definitionLabel: UILabel = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
nameLabel.textColor = UIColor.black
nameLabel.font = UIFont.systemFont(ofSize: 15)
nameLabel.textAlignment = NSTextAlignment.left
self.contentView.addSubview(nameLabel)
definitionLabel.textColor = UIColor.brown
definitionLabel.font = UIFont.systemFont(ofSize: 15)
definitionLabel.textAlignment = NSTextAlignment.right
self.contentView.addSubview(definitionLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
nameLabel.frame = CGRect(x: 10, y: 10, width: self.contentView.frame.size.width - 20, height: self.contentView.frame.size.height - 20)
definitionLabel.frame = CGRect(x: 10, y: 10, width: self.contentView.frame.size.width - 20, height: self.contentView.frame.size.height - 20)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
注册单元:
self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
根据模型使用
CustomTableViewCell
:override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Configure the cell...
if model.type == "name" {
cell.nameLabel.text = model.name
cell.definitionLabel.isHidden = true
}else if model.type == "definition" {
cell.definitionLabel.text = model.definition
cell.nameLabel.isHidden = true
}
return cell
}
移动单元格部分:
let mutableArray:[model] = [model1, model2, model3, model4, model5] // model class holds data for each cell
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let source = sourceIndexPath.row;
let destination = destinationIndexPath.row;
let sourceModel = mutableArray[source]
let destinationModel = mutableArray[destination]
mutableArray[source] = destinationModel
mutableArray[destination] = sourceModel
}
如果您对此实施有任何疑问,请随时发表评论。随意建议修改以使其更好:)
编辑:-
即使它是具有索引位置的元组,也可以在行相应移动时更改值。
var nameList : [(String, Int)] = [("Trojan", 0),
("Virus", 1),
("Spyware", 2),
("Ransomware", 3),
("Leakware", 4)]
let source = sourceIndexPath.row
let destination = destinationIndexPath.row
var sourceTuple: (String, Int) = nameList[source]
var destinationTuple: (String, Int) = nameList[destination]
sourceTuple.1 = destination
destinationTuple.1 = source
nameList[destination] = sourceTuple
nameList[source] = destinationTuple
关于ios - 小UITableView,删除dequeReuseableCell,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41697286/