lmiguelvargasf
lmiguelvargasf

Reputation: 69963

Swift: Inheritance of xib files

I have two xib files which almost the same.

VoucherTableViewCell.xib enter image description here

BrandTableViewCell.xib enter image description here

As you can see in VoucherTableViewCell I have two views "Código: AEB" and "Obtenla por". Now I have two classes for each VoucherTableViewCell and BrandTableViewCell and both have repetitive:

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!)
}

}

As you can see a lot of attributes and methods are repeated in both classes, and I would like to know how I can avoid this issue to reutilize my code as much as I can because when I am loading each element for both voucher and brand the code is similar.

What can I do? I have no idea because I am a newbie in iOS development.

Thanks in advance.

Upvotes: 3

Views: 1265

Answers (1)

Midhun MP
Midhun MP

Reputation: 107231

I would suggest you to implement a common protocol and base class that holds the common functions & properties. And make your tableviewcell class inherit from the newly created one.

// 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?
}

If you need to add more methods to your protocol, you can inherit the protocol like this:

// Adding more functions to the existing protocol
protocol VoucherTableViewCellDelegate: CommonTableViewCellDelegate
{
   func navigateToBrand()
}

Inherit your custom tableview cell class from the newly created base cell class:

class BrandTableViewCell: CommonTableViewCell
{
   ...
}


class VoucherTableViewCell: CommonTableViewCell
{
   ...
}

Upvotes: 2

Related Questions