Swift:继承xib文件

时间:2015-11-12 15:31:04

标签: ios swift uitableview xib

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

VoucherTableViewCell.xib enter image description here

BrandTableViewCell.xib enter image description here

正如你在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开发的新手。

提前致谢。

1 个答案:

答案 0 :(得分:2)

我建议你实现一个包含常用功能的通用协议和基类。属性。并使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()
}

从新创建的基本单元类继承自定义tableview单元类:

class BrandTableViewCell: CommonTableViewCell
{
   ...
}


class VoucherTableViewCell: CommonTableViewCell
{
   ...
}