gpt4 book ai didi

ios - Swift:xib文件的继承

转载 作者:搜寻专家 更新时间:2023-10-31 22:48:08 24 4
gpt4 key购买 nike

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

VoucherTableViewCell.xib enter image description here

BrandTableViewCell.xib enter image description here

如您在 VoucherTableViewCell 中所见,我有两个 View “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 开发的新手。

提前致谢。

最佳答案

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

如果你需要在你的协议(protocol)中添加更多的方法,你可以像这样继承协议(protocol):

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

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

class BrandTableViewCell: CommonTableViewCell
{
...
}


class VoucherTableViewCell: CommonTableViewCell
{
...
}

关于ios - Swift:xib文件的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33675165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com