zoukankan      html  css  js  c++  java
  • 优化tableView加载cell与model的过程

    优化tableView加载cell与model的过程

    效果图

    说明

    1. 用多态的特性来优化tableView加载cell与model的过程

    2. swift写起来果然要比Objective-C简洁了不少

    源码

    https://github.com/YouXianMing/Swift-TableViewDemo

    https://github.com/YouXianMing/OC-TableViewDemo

    //
    //  ViewController.swift
    //  Swift-TableViewDemo
    //
    //  Created by YouXianMing on 15/9/28.
    //  Copyright © 2015年 YouXianMing. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        let typeOneCellFlag : String! = "typeOneCellFlag"
        let typeTwoCellFlag : String! = "typeTwoCellFlag"
        
        var datasArray : NSMutableArray!
        var tableView  : UITableView!
        
        override func viewDidLoad() {
            
            super.viewDidLoad()
            
            self.initDatasArray()
            
            self.initTableView()
        }
    
        // 数据源相关
        func initDatasArray() {
        
            datasArray = NSMutableArray()
            datasArray.addObject(TypeOneModel(flag: typeOneCellFlag, cellHeight: 100, data: "TypeOneModel"))
            datasArray.addObject(TypeTwoModel(flag: typeTwoCellFlag, cellHeight: 200, data: "TypeTwoModel"))
        }
        
        // tableView相关
        func initTableView() {
        
            tableView = UITableView(frame: view.bounds, style: .Plain)
            tableView.delegate       = self
            tableView.dataSource     = self
            tableView.separatorStyle = .None
            view.addSubview(tableView)
            
            tableView.registerClass(TypeOneCell.classForCoder(), forCellReuseIdentifier: typeOneCellFlag)
            tableView.registerClass(TypeTwoCell.classForCoder(), forCellReuseIdentifier: typeTwoCellFlag)
        }
        
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
            return datasArray.count
        }
        
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
            let model : BaseModel! = datasArray[indexPath.row] as! BaseModel
            
            let cell : BaseTableViewCell! = tableView.dequeueReusableCellWithIdentifier(model.cellFlag!) as! BaseTableViewCell
            cell.tableView                = tableView
            cell.indexPath                = indexPath
            cell.data                     = model.data
            cell.loadData()
            
            return cell
        }
        
        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            
            let model : BaseModel! = datasArray[indexPath.row] as! BaseModel
            
            return model.cellHeight!
        }
    }

    细节

  • 相关阅读:
    面向消息的持久通信与面向流的通信
    通信协议
    分布式系统简介
    Hadoop on Yarn 各组件详细原理
    Parquet文件结构笔记
    Redis部分数据结构方法小结
    Storm Ack框架笔记
    MapReduce格式与类型
    Hadoop 2.6 MapReduce运行原理详解
    Hadoop SequenceFile数据结构介绍及读写
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4845087.html
Copyright © 2011-2022 走看看