美文网首页
swift - 工厂模式根据model创建不同cell

swift - 工厂模式根据model创建不同cell

作者: ZackDt | 来源:发表于2017-11-15 16:34 被阅读0次

思路简单说明

工厂模式可以简单理解成:根据接口,创建不同的对象。
在实际项目中,可能需获取Model数组, 再由tableViewCell呈现到界面给用户(CollectionView同理).我们得知道自己需要什么, 当然工厂的产品应该是不同种类的cell,。根据不同的model, 创建一个cellFactory, 放入Model, 进行处理, 生成不同的cell。

具体代码

下面贴上具体代码

2.1工厂代码:

//
//  ShopCellFactory.swift
//  @018使用工厂模式的TableView
//
//  Created by Zack on 2017/11/15.
//  Copyright © 2017年 Zack. All rights reserved.
//

import UIKit


// 可选实现协议方法
@objc protocol ShopGoodsProtocol:NSObjectProtocol {
    // 也可以不可选,看需求
    @objc optional var model: ShopModel { get set}
    
    @objc optional func chooseShopGoods(cell: ShopGoodsProtocol, btn: UIButton)
    @objc optional func addShopGoods(cell: ShopGoodsProtocol, btn: UIButton)
    @objc optional func minusShopGoods(cell: ShopGoodsProtocol, btn: UIButton)
    @objc optional func showShopInf(cell: ShopGoodsProtocol, imgV: UIImageView)
}

extension ShopGoodsProtocol {
    //这里可以写一些默认实现
    func showShopInf(cell: ShopGoodsProtocol, imgV: UIImageView) {
        print(model?.imageName ?? "图片")
    }
}

// 具体的工厂:
struct ShopFactory {
    static func createCell(model: ShopModel, tableView: UITableView, indexPath: IndexPath) -> ShopGoodsProtocol? {
        switch model.id {
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: model.imageName!, for: indexPath) as! NormalCell
            cell.configCell(model)
            return cell
        case 2:
            let cell = tableView.dequeueReusableCell(withIdentifier: model.imageName!, for: indexPath) as! CarryGiftCell
            cell.configCell(model)
            return cell
        case 3:
            let cell = tableView.dequeueReusableCell(withIdentifier: model.imageName!, for: indexPath) as! FullGiftCell
            cell.configCell(model)
            return cell
        case 4:
            let cell = tableView.dequeueReusableCell(withIdentifier: model.imageName!, for: indexPath) as! StockoutCell
            cell.configCell(model)
            return cell
        default:
                return nil
        }
    }
}

备注:这里只是简单演示了下,为了方便都设为可选,而且具体cell中也没有实现。

2.2其中的一个cell:

class CarryGiftCell: UITableViewCell , ShopGoodsProtocol {

    func configCell(_ model: ShopModel) {
        self.textLabel?.text = model.title
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = UIColor.yellow
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

2.3控制器:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = ShopFactory.createCell(model: self.dataArray[indexPath.row], tableView: tableView, indexPath: indexPath)
        return cell as! UITableViewCell
    }

效果

QQ20171115-162918@2x.png

具体代码:
工厂模式创建不同cell

相关文章

  • swift - 工厂模式根据model创建不同cell

    思路简单说明 工厂模式可以简单理解成:根据接口,创建不同的对象。在实际项目中,可能需获取Model数组, 再由ta...

  • js设计模式-工厂模式

    工厂模式 什么是工厂模式工厂模式是一种 创建模式,用来解决创建对象的问题。根据参数类型,通过调用工厂方法来创建不同...

  • Cell工厂设计

    Cell工厂设计 关于一个Cell工厂设计模式的 Demo Model层 .首先建立 BaseModel并继承他建...

  • 设计模式之工厂模式

    1. 简单工厂 简单工厂模式又称为静态工厂模式,它属于创建型模式。在简单工厂模式中,可以根据不同的参数返回不同类的...

  • Android 设计模式 - 工厂模式

    1. 简单工厂 简单工厂模式又称为静态工厂模式,它属于创建型模式。在简单工厂模式中,可以根据不同的参数返回不同类的...

  • 简单工厂模式

    介绍简单工厂模式属于创建型模式又叫做静态工厂方法模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回...

  • 设计模式学习--简单工厂模式

    模式定义 又称静态工厂方法模式,它属于创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂模...

  • JS设计模式—工厂模式(Factory Pattern)

    什么是工厂模式? 工厂模式是一种 创建模式,用来解决创建对象的问题。根据参数类型,通过调用工厂方法来创建不同类型的...

  • 策略模式-Golang实现

    目的:根据不同策略来执行对象的相应操作 和工厂模式很像,不同点在于:工厂模式是传入参数后创建对象,根据传入的参数写...

  • 设计模式之简单工厂模式(创建型)

    模式定义 简单工厂模式又称静态工厂模式。可以根据参数的不同返回不同类的实例。定义一个类来创建其它类的实例。 模式角...

网友评论

      本文标题:swift - 工厂模式根据model创建不同cell

      本文链接:https://www.haomeiwen.com/subject/kzchvxtx.html