swift-UITableView

Swift UITableView 示例
本文提供了一个使用Swift实现UITableView的示例代码,展示了如何设置UITableView的数据源和代理,包括单元格的复用、高度设置及点击响应等功能。
import UIKit

class FirstVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView : UITableView?
    var items = ["武汉","上海","武汉","上海","武汉","上海","武汉","上海"]

    override func viewDidLoad() {
        super.viewDidLoad()

        initView()
        
        // Do any additional setup after loading the view.
    }
    
    
    func initView(){
        
        self.tableView = UITableView(frame:self.view.frame,style:UITableViewStyle.plain)
        self.tableView!.dataSource = self
        self.tableView!.delegate = self
        self.tableView!.register(FirstCell.classForCoder(), forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView!)
        self.tableView?.tableFooterView = UIView()
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
//        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        //        cell.textLabel!.text = self.items[indexPath.row]
        let cell:FirstCell! = tableView.dequeueReusableCell(withIdentifier: "cell") as! FirstCell!
        cell.titles.text = self.items[indexPath.row]

        
        //去除cell阴影
        cell.selectionStyle = UITableViewCellSelectionStyle.none

        return cell
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        print("点击cell的indexpath.row:\(items[indexPath.row])")
        
        let detail = First_detail_VC()
        self.navigationController?.pushViewController(detail, animated: true)
        
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

 

 

转载于:https://www.cnblogs.com/sayimba/p/6215131.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值