ios 中具有共同特征的自定义单元格
Custom cells with common features in ios
我有一个应用程序,其中有一个表格视图。在这个表格视图中,我目前有 8 种不同类型的单元格。这些细胞中的每一个都是不同的,但也有相似之处。
作为示例(为简单起见,仅使用 2 个单元格)。假设您有一个单元格 1,它可以分为 3 个部分:
----------------A------------
--------------乙--------------
--------------C-------------
现在单元格 2 也有 3 个部分。 A和C部分与cell1中的相同(当然数据不同,但结构相同):
----------------A------------
--------------D--------------
--------------C-------------
目前,在我的 cellForRowAt 方法中,我只检查单元格类型应该是 1 还是 2,然后我将该类型的单元格出列。挑战在于我想避免在代码中的两个不同位置设置 A 部分和 C 部分。
例如。而不是
if type == type1 {
//Set A
//Set B
//Set C
} else if type == type2 {
//Set A
//Set D
//Set C
}
我愿意
//Set A
//Set C
if type == type1 {
//Set B
} else if type == type2 {
//Set D
}
我想知道是否有办法 "abstract" 这些共性?
如有不明之处请告诉我。
编辑:IB 挑战
我可能还应该提到,对我来说一个棘手的部分是弄清楚父细胞如何适应 IB。对于每种细胞类型,我都有单独的 xib 文件,但是如果我只有一个包含 A 和 C 部分的父 swift 文件,我的其他 xib 文件是否可以具有与该父级相同的出口,甚至如何完成?
Parent TableViewCell
class BaseTableViewCell: UITableViewCell {
@IBOutlet weak var ALabel: UILabel!
@IBOutlet weak var CLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Subclass of BaseCell
class TypeOneTableViewCell: BaseTableViewCell {
@IBOutlet weak var BLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我有一个应用程序,其中有一个表格视图。在这个表格视图中,我目前有 8 种不同类型的单元格。这些细胞中的每一个都是不同的,但也有相似之处。
作为示例(为简单起见,仅使用 2 个单元格)。假设您有一个单元格 1,它可以分为 3 个部分:
----------------A------------ --------------乙-------------- --------------C-------------
现在单元格 2 也有 3 个部分。 A和C部分与cell1中的相同(当然数据不同,但结构相同):
----------------A------------ --------------D-------------- --------------C-------------
目前,在我的 cellForRowAt 方法中,我只检查单元格类型应该是 1 还是 2,然后我将该类型的单元格出列。挑战在于我想避免在代码中的两个不同位置设置 A 部分和 C 部分。
例如。而不是
if type == type1 {
//Set A
//Set B
//Set C
} else if type == type2 {
//Set A
//Set D
//Set C
}
我愿意
//Set A
//Set C
if type == type1 {
//Set B
} else if type == type2 {
//Set D
}
我想知道是否有办法 "abstract" 这些共性?
如有不明之处请告诉我。
编辑:IB 挑战
我可能还应该提到,对我来说一个棘手的部分是弄清楚父细胞如何适应 IB。对于每种细胞类型,我都有单独的 xib 文件,但是如果我只有一个包含 A 和 C 部分的父 swift 文件,我的其他 xib 文件是否可以具有与该父级相同的出口,甚至如何完成?
Parent TableViewCell
class BaseTableViewCell: UITableViewCell {
@IBOutlet weak var ALabel: UILabel!
@IBOutlet weak var CLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Subclass of BaseCell
class TypeOneTableViewCell: BaseTableViewCell {
@IBOutlet weak var BLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}