美文网首页
swiftMVC模式自定义表格

swiftMVC模式自定义表格

作者: ViewController_ | 来源:发表于2018-10-25 19:13 被阅读0次

viewcontoller里写左右侧滑了,所以主界面是v1viewcontoller。

创建MVC:(使用xib)

然后就写model,(只举个例子)

//拖xib(也是举个例子)

所以最后回到主界面v1viewcontoller:

首先在上面创建数组:

 // 第一组数据
    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] = []
   

然后在viewDidLoad()里写表格:

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(titleo:onelabel1[i],shijiano:onelabel2[i])

            mArr.append(md1)

        }

        let md2 = twomodel(titlei: twolabel1, imgi: twoimgArr)

        mArr.append(md2)

        for i in 0..<onelabel1.count {

            let md3 = threemodel(titleu: threelabel1[i], imgu1: threeimgArr1[i], imgu2: threeimgArr2[i])

            mArr.append(md3)

        }

然后表格添加到视图

self.view.addSubview(tbv)

}

最后写表格里的内容

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

    }

在下边

相关文章

网友评论

      本文标题:swiftMVC模式自定义表格

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