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

思路简单说明

工厂模式可以简单理解成:根据接口,创建不同的对象。
在实际项目中,可能需获取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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,300评论 4 61
  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 5,332评论 1 23
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,407评论 0 1
  • 本期作业: 目前所有人都在唱衰线下零售,以百货商场为主。 请调研:是否线下零售业真的不行了,如何才能挽救或更加蓬勃...
    寜少阅读 267评论 1 0
  • 一只猫得旅行阅读 674评论 0 1