[英]Swift - Selecting cells when having multiple custom cell-classes
I have 2 custom cells that I need to set the selection properties for. 我需要设置2个自定义单元格的选择属性。 Right now, I've used a boolean to tell the program that the cell is either type 1 or type 2, because I was just checking if the code was working. 现在,我使用了一个布尔值来告诉程序该单元格是1型还是2型,因为我只是在检查代码是否正常工作。
Now I need the program to find out which type of cell it is by itself, and I don't know how it's done. 现在,我需要该程序来找出它本身是哪种类型的单元格,而我不知道它是如何完成的。
My code looks like this: 我的代码如下所示:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if istrue{
let mySelectedCell: CustomTableCell = tv.cellForRowAtIndexPath(indexPath)! as CustomTableCell
if (mySelectedCell.accessoryType == UITableViewCellAccessoryType.Checkmark) {
mySelectedCell.accessoryType = UITableViewCellAccessoryType.None
if let tx = mySelectedCell.customLabel.text as String!{
var textLabel:String = String()
textLabel = tx
println(textLabel)
if let ind = find(tempList, textLabel) {
tempList.removeAtIndex(ind)
println(tempList)
}
}
}
else {
mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
mySelectedCell.tintColor = UIColor.blackColor()
if let tx = mySelectedCell.customLabel.text as String!{
var textLabel:String = String()
textLabel = tx
tempList.append(textLabel)
println(tempList)
}
}
}else{
let mySelectedCell: CustomTableCell2 = tv.cellForRowAtIndexPath(indexPath)! as CustomTableCell2
if (mySelectedCell.accessoryType == UITableViewCellAccessoryType.Checkmark) {
mySelectedCell.accessoryType = UITableViewCellAccessoryType.None
if let tx = mySelectedCell.titleLabel.text as String!{
var textLabel:String = String()
textLabel = tx
println(textLabel)
if let ind = find(tempList, textLabel) {
tempList.removeAtIndex(ind)
println(tempList)
}
}
}
else {
mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
mySelectedCell.tintColor = UIColor.blackColor()
if let tx = mySelectedCell.titleLabel.text as String!{
var textLabel:String = String()
textLabel = tx
tempList.append(textLabel)
println(tempList)
}
}
}
}
I don't know if this is the best way of doing it either, so please give me some tips if you have a better way to set this "checkmark" selection-style. 我也不知道这是否是最好的方法,因此,如果您有更好的方法来设置此“选中标记”选择样式,请给我一些提示。
All suggestions on how to achieve this would be appreciated. 关于如何实现这一点的所有建议将不胜感激。
Here is a Objective example of this: 这是一个客观的例子:
id cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[PhotoCell2x class]]) {
NSLog(@"1");
}
else if([cell isKindOfClass:[EffectCell2x class]]){
NSLog(@"2");
}
If I get time i'll add a swift version. 如果我有时间,我会添加一个快速版本。 Hope this helps.. :) 希望这可以帮助.. :)
Here it is updated for Swift 4 for anyone that needs it. 在这里,它针对需要它的任何人针对Swift 4进行了更新。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if (cell?.isMember(of: CLASSNAME.self))!{
print("hello")
} else {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.