【问题标题】:Swift: Inheritance of xib filesSwift:xib 文件的继承
【发布时间】:2015-11-12 15:31:03
【问题描述】:

我有两个几乎相同的 xib 文件。

VoucherTableViewCell.xib

BrandTableViewCell.xib

正如您在 VoucherTableViewCell 中看到的,我有两个视图“Código: AEB”和“Obtenla por”。现在我为每个 VoucherTableViewCellBrandTableViewCell 有两个类,并且都有重复:

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 开发的新手。

提前致谢。

【问题讨论】:

  • 为什么不能给你写一个继承uitableviewcell的通用基类,让你的tableviewcell类继承新创建的类?
  • @MidhunMP 我正在考虑这个问题,但在这种情况下,我是否必须为那个超类创建一个 xib 文件?
  • 不需要为此创建 xib
  • 那么,我如何关联例如应该在超类中创建的brandImage 视图,但我没有要连接的xib?我的意思是,如何指示超类中的属性与其子类之一的 xib 文件中的某些内容相关。
  • 子类将具有它的超类的所有属性。所以你可以使用它。

标签: ios swift uitableview xib


【解决方案1】:

我建议您实现一个通用协议和基类来保存通用功能和属性。并让您的 tableviewcell 类继承新创建的类。

// 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 BrandTableViewCell: CommonTableViewCell
{
   ...
}


class VoucherTableViewCell: CommonTableViewCell
{
   ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多