Swift: xib文件的继承
Swift: Inheritance of xib files
我有两个几乎相同的 xib 文件。
VoucherTableViewCell.xib
BrandTableViewCell.xib
如您在 VoucherTableViewCell 中所见,我有两个视图 "Código: AEB" 和 "Obtenla por"。现在我有两个 类 每个 VoucherTableViewCell 和 BrandTableViewCell 并且都有重复:
VoucherTableViewCell.swift
protocol VoucherTableViewCellDelegate: class {
func detailOptionTapped(id: String, description: String)
func likeTapped(voucherId: String, reserved:Bool?, indexPath:NSIndexPath)
func navigateToBrand()
}
class VoucherTableViewCell: UITableViewCell {
var delegate:VoucherTableViewCellDelegate?
var voucherId:String?
var reserved:Bool?
var indexPath:NSIndexPath?
var brandId: String!
@IBOutlet weak var voucherImageView: UIImageView!
@IBOutlet weak var codeView: UIView!
@IBOutlet weak var codeLabel: UILabel!
@IBOutlet weak var hotDealView: UIView!
@IBOutlet weak var hotDealLabel: UILabel!
@IBOutlet weak var likeView: UIView!
@IBOutlet weak var likeIconLabel: UILabel!
@IBOutlet weak var likeNumberLabel: UILabel!
@IBOutlet weak var brandLogoImageView: UIImageView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var detailView: UIView!
@IBOutlet weak var detailIconLabel: UILabel!
@IBOutlet weak var hotDealFlameIconLabel: UILabel!
@IBOutlet weak var smartCoinIconLabel: UILabel!
@IBAction func navigateToVoucherDetails(sender: UIButton) {
self.delegate?.detailOptionTapped(voucherId!,description: descriptionLabel.text!)
}
@IBAction func likeTapped(sender:UIButton) {
self.delegate?.likeTapped(voucherId!, reserved: reserved, indexPath: indexPath!)
}
@IBAction func navigateToBrand(sender: UIButton) {
self.delegate?.navigateToBrand()
}
}
BrandTableViewCell.swift
protocol BrandTableViewCellDelegate: class {
func showBrandDetails(id: String, description: String)
func likeTapped(brandId: String, reserved:Bool?, indexPath:NSIndexPath)
}
class BrandTableViewCell: UITableViewCell {
var delegate:BrandTableViewCellDelegate?
var brandId:String?
var reserved:Bool?
var indexPath:NSIndexPath?
@IBOutlet weak var brandImageView: UIImageView!
@IBOutlet weak var likeButtonView: UIView!
@IBOutlet weak var likeNumberLabel: UILabel!
@IBOutlet weak var likeIconLabel: UILabel!
@IBOutlet weak var brandLogoImageView: UIImageView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var detailButtonView: UIView!
@IBOutlet weak var detailIconLabel: UILabel!
@IBAction func showBrandDetails(sender: UIButton) {
self.delegate?.showBrandDetails(brandId!,description: descriptionLabel.text!)
}
@IBAction func likeTapped(sender:UIButton) {
self.delegate?.likeTapped(brandId!, reserved: reserved, indexPath: indexPath!)
}
}
如您所见,类 中重复了很多属性和方法,我想知道如何避免这个问题以尽可能多地重新利用我的代码,因为当我为优惠券和品牌加载每个元素的代码是相似的。
我能做什么?我不知道,因为我是 iOS 开发的新手。
提前致谢。
我建议您实现一个通用协议和包含通用功能和属性的基础 class。并使您的 tableviewcell class 继承自新创建的。
// Common Protocol
protocol CommonTableViewCellDelegate: class
{
func showDetailsTapped(id: String, description: String)
func likeTapped(commonId: String, reserved:Bool?, indexPath:NSIndexPath)
}
// Common Base class
class CommonTableViewCell: UITableViewCell
{
var cId:String?
var reserved:Bool?
var indexPath:NSIndexPath?
}
如果你需要为你的协议添加更多的方法,你可以像这样继承协议:
// Adding more functions to the existing protocol
protocol VoucherTableViewCellDelegate: CommonTableViewCellDelegate
{
func navigateToBrand()
}
从新创建的基本单元格 class:
继承您的自定义表格视图单元格 class
class BrandTableViewCell: CommonTableViewCell
{
...
}
class VoucherTableViewCell: CommonTableViewCell
{
...
}
我有两个几乎相同的 xib 文件。
VoucherTableViewCell.xib
BrandTableViewCell.xib
如您在 VoucherTableViewCell 中所见,我有两个视图 "Código: AEB" 和 "Obtenla por"。现在我有两个 类 每个 VoucherTableViewCell 和 BrandTableViewCell 并且都有重复:
VoucherTableViewCell.swift
protocol VoucherTableViewCellDelegate: class {
func detailOptionTapped(id: String, description: String)
func likeTapped(voucherId: String, reserved:Bool?, indexPath:NSIndexPath)
func navigateToBrand()
}
class VoucherTableViewCell: UITableViewCell {
var delegate:VoucherTableViewCellDelegate?
var voucherId:String?
var reserved:Bool?
var indexPath:NSIndexPath?
var brandId: String!
@IBOutlet weak var voucherImageView: UIImageView!
@IBOutlet weak var codeView: UIView!
@IBOutlet weak var codeLabel: UILabel!
@IBOutlet weak var hotDealView: UIView!
@IBOutlet weak var hotDealLabel: UILabel!
@IBOutlet weak var likeView: UIView!
@IBOutlet weak var likeIconLabel: UILabel!
@IBOutlet weak var likeNumberLabel: UILabel!
@IBOutlet weak var brandLogoImageView: UIImageView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var detailView: UIView!
@IBOutlet weak var detailIconLabel: UILabel!
@IBOutlet weak var hotDealFlameIconLabel: UILabel!
@IBOutlet weak var smartCoinIconLabel: UILabel!
@IBAction func navigateToVoucherDetails(sender: UIButton) {
self.delegate?.detailOptionTapped(voucherId!,description: descriptionLabel.text!)
}
@IBAction func likeTapped(sender:UIButton) {
self.delegate?.likeTapped(voucherId!, reserved: reserved, indexPath: indexPath!)
}
@IBAction func navigateToBrand(sender: UIButton) {
self.delegate?.navigateToBrand()
}
}
BrandTableViewCell.swift
protocol BrandTableViewCellDelegate: class {
func showBrandDetails(id: String, description: String)
func likeTapped(brandId: String, reserved:Bool?, indexPath:NSIndexPath)
}
class BrandTableViewCell: UITableViewCell {
var delegate:BrandTableViewCellDelegate?
var brandId:String?
var reserved:Bool?
var indexPath:NSIndexPath?
@IBOutlet weak var brandImageView: UIImageView!
@IBOutlet weak var likeButtonView: UIView!
@IBOutlet weak var likeNumberLabel: UILabel!
@IBOutlet weak var likeIconLabel: UILabel!
@IBOutlet weak var brandLogoImageView: UIImageView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var detailButtonView: UIView!
@IBOutlet weak var detailIconLabel: UILabel!
@IBAction func showBrandDetails(sender: UIButton) {
self.delegate?.showBrandDetails(brandId!,description: descriptionLabel.text!)
}
@IBAction func likeTapped(sender:UIButton) {
self.delegate?.likeTapped(brandId!, reserved: reserved, indexPath: indexPath!)
}
}
如您所见,类 中重复了很多属性和方法,我想知道如何避免这个问题以尽可能多地重新利用我的代码,因为当我为优惠券和品牌加载每个元素的代码是相似的。
我能做什么?我不知道,因为我是 iOS 开发的新手。
提前致谢。
我建议您实现一个通用协议和包含通用功能和属性的基础 class。并使您的 tableviewcell class 继承自新创建的。
// Common Protocol
protocol CommonTableViewCellDelegate: class
{
func showDetailsTapped(id: String, description: String)
func likeTapped(commonId: String, reserved:Bool?, indexPath:NSIndexPath)
}
// Common Base class
class CommonTableViewCell: UITableViewCell
{
var cId:String?
var reserved:Bool?
var indexPath:NSIndexPath?
}
如果你需要为你的协议添加更多的方法,你可以像这样继承协议:
// Adding more functions to the existing protocol
protocol VoucherTableViewCellDelegate: CommonTableViewCellDelegate
{
func navigateToBrand()
}
从新创建的基本单元格 class:
继承您的自定义表格视图单元格 classclass BrandTableViewCell: CommonTableViewCell
{
...
}
class VoucherTableViewCell: CommonTableViewCell
{
...
}