Swift4.0 学习笔记 第十节:UITableView

本文介绍了一个使用Swift实现TableView的基本示例,包括如何创建TableView、设置数据源、注册单元格及显示内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  ViewController.swift
//  013-tableView
//
//  Created by 庄壮勇 on 2018/1/4.
//  Copyright © 2018年 Personal. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
       
        // 提示: textLabel 是可选的
        // 代码中 ? 是自动带的, 如果textLabel 有,就使用, 如果没有,就什么也不做
        cell.textLabel?.text = "hello ---- \(indexPath.row)"
        return cell
       
    }
   

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    func setupUI() {
       
        // 1. 创建表格
        let tv = UITableView(frame: view.bounds, style: .plain)
       
        // 2. 添加到视图
        view.addSubview(tv)
       
        // 3. 注册可重用 cell [UITableView class]
        tv.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
       
        // 4. 设置数据源
        // Swift 中没有遵守协议是一个错误
        tv.dataSource = self
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值