美文网首页
用swift写自定义表格

用swift写自定义表格

作者: 盖世英雄的梦想 | 来源:发表于2018-10-26 01:10 被阅读0次

    首先我们先在AppDelegate里创建导航条和UITablebar

    self.window?.rootViewController = UINavigationController(rootViewController: ViewController())

        let one = ViewController()
        
        let nav = UINavigationController(rootViewController: one)
        
        nav.tabBarItem = UITabBarItem(title: "hh", image: UIImage(named: ""), tag: 1)
    
        let er = erViewController()
        
        let nav1 = UINavigationController(rootViewController: er)
        
        nav1.tabBarItem = UITabBarItem(title: "hh", image: UIImage(named: ""), tag: 1)
    
        let tab = UITabBarController()
        tab.viewControllers = [nav,nav1]
    
        self.window?.rootViewController = tab
    

    然后创建MVC模式

    在Model里创建三个model
    在第一个model中写
    var TitleL:String
    var ZhiL:String
    var NewsL:String
    var PingL:String
    var TimeL:String

    init(TitleL:String,ZhiL:String,NewsL:String,PingL:String,TimeL:String) {
        
        self.TitleL = TitleL
        self.ZhiL = ZhiL
        self.PingL = PingL
        self.NewsL = NewsL
        self.TimeL = TimeL
        
    }
    

    在第二个model中写
    var title1:String
    var hot1:String
    var news1:String
    var ping1:String
    var time1:String
    var img1:String
    init(title1:String,hot1:String,news1:String,ping1:String,time1:String,img1:String)
    {
    self.title1 = title1
    self.hot1 = hot1
    self.news1 = news1
    self.ping1 = ping1
    self.time1 = time1
    self.img1 = img1

    }
    

    在第三个model中写
    var table1 : String
    var imgname1 : String
    var imgname2 : String
    var imgname3 : String

    init(table1:String,imgname1:String,imgname2:String,imgname3:String) {
        
        self.table1 = table1
        self.imgname1 = imgname1
        self.imgname2 = imgname2
        self.imgname3 = imgname3
        
    }
    

    接着在view中创建三个UItableviewcell并绘制xib

    第一个cell

    @IBOutlet weak var lab1: UILabel!
    @IBOutlet weak var lab2: UILabel!

    func set1(model:oneModel) -> Void {
        self.lab1.text = model.TitleL
        self.lab2.text = model.ZhiL
        
    }
    

    第二个cell
    @IBOutlet weak var labo1: UILabel!
    @IBOutlet weak var labo2: UILabel!

    @IBOutlet weak var imgo1: UIImageView!
    
    
    func set2(model:twoModel) -> Void {
        
        self.labo1.text = model.title1
        self.labo2.text = model.hot1
        self.imgo1.image = UIImage (named:model.img1)
        
    }
    

    第三个cell
    @IBOutlet weak var labi1: UILabel!

    @IBOutlet weak var imgi1: UIImageView!
    
    @IBOutlet weak var imgi2: UIImageView!
    
    @IBOutlet weak var imgi3: UIImageView!
    
    
    func set3(model:threeModel) -> Void {
        self.labi1.text = model.table1
        self.imgi1.image = UIImage (named: model.imgname1)
        self.imgi2.image = UIImage (named: model.imgname2)
        self.imgi3.image = UIImage (named: model.imgname3)
        
    }
    

    然后在oneViewController写

    class oneViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{

    // 第一组数据
    let onelabel1 = ["",""]
    let onelabel2 = ["",""]
    let onelabel3 = ["",""];
    let onelabel4 = ["",""]
    let onelabel5 = ["",""]
    //第二组数据
    let twolabel1 = ""
    let twolabel2 = ""
    let twolabel3 = ""
    let twolabel4 = ""
    let twolabel5 = ""
    let twoimgArr = "1"
    // 第三组数据
    let threelabel1 = ["但是你得酷暑发的难当看粉色的","都很骄傲的骄傲看"]
    let threeimgArr1 = ["1","1"]
    let threeimgArr2 = ["1","1"]
    let threeimgArr3 = ["1","1"]
    
    let typeArr:[Int] = [1,1,2,3,3]
    var mArr:[AnyObject] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Do any additional setup after loading the view.
    
        let  tbv = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
        
        tbv.delegate = self
        tbv.dataSource = self
        
        tbv.register(UINib(nibName:"oneTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell1")
        tbv.register(UINib(nibName:"twoTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell2")
        tbv.register(UINib(nibName: "threeTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell3")
    
    
        for i in 0..<onelabel1.count {
            
            let md1 = oneModel(TitleL:onelabel1[i],ZhiL:onelabel1[i],NewsL:onelabel4[i], PingL:onelabel3[i],TimeL:onelabel5[i])
            
            mArr.append(md1)
        }
        
        
        let md2 = twoModel(title1: twolabel1, hot1: twolabel2, news1: twolabel3, ping1: twolabel4, time1: twolabel5, img1: twoimgArr)
        
        mArr.append(md2)
        
        
        for i in 0..<onelabel1.count {
            
            let md3 = threeModel(table1: threelabel1[i], imgname1: threeimgArr1[i], imgname2: threeimgArr2[i], imgname3: threeimgArr3[i])
            
            mArr.append(md3)
        }
        
        
        
        self.view.addSubview(tbv)
        
    
    
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let type:Int = typeArr[indexPath.row]
        if (type == 1)
        {
            let cell:oneTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell1")as!oneTableViewCell
            let md1:oneModel = mArr[indexPath.row]as!oneModel
            cell.set1(model: md1)
            return cell
        }else if (type == 2)
        {
            let cell:twoTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell2")as!twoTableViewCell
            let md2:twoModel = mArr[indexPath.row]as!twoModel
            cell.set2(model: md2)
            
            return cell
        }else
        {
            let cell:threeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell3")as!threeTableViewCell
            let md3:threeModel = mArr[indexPath.row]as!threeModel
            cell.set3(model: md3)
            return cell
        }
        
        
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 130
    }
    

    相关文章

      网友评论

          本文标题:用swift写自定义表格

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