美文网首页
Adapter Design Patterns(适配器模式)

Adapter Design Patterns(适配器模式)

作者: 863cda997e42 | 来源:发表于2017-09-22 16:48 被阅读20次

    一个列表视图,加载不同的数据,为了使模型和视图分开可以使用适配器模式。图书,音乐,电影三个列表使用同一个列表进行展示,三者有各自不同的数据格式。
    代码如下:

    class BookModel: NSObject {
        
        var name: String!
        var author: String!
        var cover: String!
        
        init(name: String, author: String, cover: String) {
            super.init()
            self.name = name
            self.author = author
            self.cover = cover
        }
    }
    
    class MusicModel: NSObject {
        var name: String!
        var singer: String!
        var album: String!
        
        init(name: String, singer: String, album: String) {
            super.init()
            self.name = name
            self.singer = singer
            self.album = album
        }
    }
    
    class MovieModel: NSObject {
        
        var name: String!
        var director: String!
        var poster: String!
        
        init(name: String, director: String, poster: String) {
            super.init()
            self.name = name
            self.director = director
            self.poster = poster
        }
    
    }
    
    

    列表视图包含 一个imageView,两个label。
    定义一个协议 CustomCellAdapterProtocol

    protocol CustomCellAdapterProtocol {
        func title() -> String?
        func subTitle() -> String?
        func image() -> UIImage?
    }
    
    

    定义一个类实现该协议但不写具体方法,作为基类。

    class CustomCellAdapter: NSObject, CustomCellAdapterProtocol {
        
        var data: Any!
    
        func title() -> String? {
            return nil
        }
    
        func subTitle() -> String? {
            return nil
        }
        
        func image() -> UIImage? {
            return nil
        }
    }
    
    

    然后编写模型适配器类

    
    class ModelAdapter: CustomCellAdapter {
    
        init(data: Any) {
            super.init()
            self.data = data
        }
        
        override func title() -> String? {
            if ((data as? BookModel) != nil) {
                let model = data as! BookModel
                return model.name
            } else if ((data as? MusicModel) != nil){
                let model = data as! MusicModel
                return model.name
            } else {
                let model = data as! MovieModel
                return model.name
            }
           
            
        }
        
        override func subTitle() -> String? {
            if ((data as? BookModel) != nil) {
                let model = data as! BookModel
                return model.author
            } else if ((data as? MusicModel) != nil){
                let model = data as! MusicModel
                return model.singer
            } else {
                let model = data as! MovieModel
                return model.director
            }
            
        }
        
        override func image() -> UIImage? {
            if ((data as? BookModel) != nil) {
                let model = data as! BookModel
                return UIImage(named: model.cover)
            } else if ((data as? MusicModel) != nil){
                let model = data as! MusicModel
                return UIImage(named: model.album)
            } else {
                let model = data as! MovieModel
                return UIImage(named: model.poster)
            }
           
        }
    }
    
    
    
    

    列表视图加载数据

    
    func loadData(data: CustomCellAdapterProtocol) {
            self.headImageView.image = data.image()
            self.headLabel.text = data.title()
            self.subHeadLabel.text = data.subTitle()
        }
    

    GitHub地址

    相关文章

      网友评论

          本文标题:Adapter Design Patterns(适配器模式)

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