Swift4.0创建列表视图UITableView

四个步骤

1.创建模拟数据源(数组)

var dataSuorceArray = ["123","456","789"]

2.声明控件

var forthTableView:UITableView

3.设置UItableview的属性并注册cell

forthTableView = UITableView(frame:self.view.bounds)
        forthTableView.delegate = self
        forthTableView.dataSource = self
        forthTableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellID")
        self.view.addSubview(forthTableView)

4.设置代理方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSuorceArray.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = (tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath))
        cell.textLabel?.text = dataSuorceArray[indexPath.row]
        return cell
    }


源码

//
//  ForthViewController.swift
//  SwiftProject
//
//  Created by sTagRD on 2018/1/3.
//  Copyright © 2018年 stag. All rights reserved.
//

import UIKit
import SwiftWebVC
class ForthViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var forthTableView:UITableView!
    var dataSuorceArray = ["123","456","789"]
    internal var nameString: String?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置该属性让contentview的x起点从navigationbar下面开始即x:64
        self.edgesForExtendedLayout = .init(rawValue: 0)
        forthTableView = UITableView(frame:self.view.bounds)
        forthTableView.delegate = self
        forthTableView.dataSource = self
        forthTableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellID")
        self.view.addSubview(forthTableView)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSuorceArray.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = (tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath))
        cell.textLabel?.text = dataSuorceArray[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值