Swift 自定义表格

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let vc = ViewController()

        let nvc = UINavigationController(rootViewController: vc)

        self.window?.rootViewController = nvc;



        return true

    }






class MyTableViewCell: UITableViewCell {

    var image1 = UIImageView()

    var time = UILabel()

    var name = UILabel()

    var names = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.contentView.addSubview(image1)

        self.contentView.addSubview(time)

        self.contentView.addSubview(name)

        self.contentView.addSubview(names)

        name.font = UIFont.systemFont(ofSize: 14)

        names.font = UIFont.systemFont(ofSize: 8)

        time.font = UIFont.systemFont(ofSize: 8)

        image1.frame = CGRect(x: 10, y: 10, width: 30, height: 30);

        name.frame = CGRect(x: 60, y: 10, width:UIScreen.main.bounds.size.width - 160, height: 50);

        names.frame = CGRect(x: 60, y: 70, width:UIScreen.main.bounds.size.width - 160, height: 50);

        time.frame = CGRect(x:UIScreen.main.bounds.size.width - 140, y: 0, width: 100, height: 20)

    }


    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

}


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var tabel = UITableView();


    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

        self.title = "asdj"

        tabel = UITableView.init(frame:UIScreen.main.bounds, style: .plain)

        tabel.delegate = self

        tabel.dataSource = self

        tabel.rowHeight = 80

        tabel.register(UINib.init(nibName: "MyTableViewCell", bundle: nil) , forCellReuseIdentifier:  "cellid")

        self.view.addSubview(tabel)


    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 3;

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell:MyTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "") as? MyTableViewCell

        if cell == nil {

            cell = MyTableViewCell(style: .subtitle, reuseIdentifier: "cellid")

        }

        cell?.image1.image = UIImage(named: "1")

        cell?.names.text = "不是假的不放假as百度金矿发布阿斯加德加多宝房价开始";

        cell?.name.text = "sjshdjkskd";

        cell?.time.text = "201730-4743"

        return cell!

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let one = OneViewController()

        //跳转

        self.navigationController?.pushViewController(one, animated: true)


    }


}

你可能感兴趣的:(Swift 自定义表格)