您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页swift-TableView初体验

swift-TableView初体验

来源:二三四教育网

尝试下swift些UI的快感,然而第一接触还是有点不顺手,一个TableView也研究了一下,方便了许多。都说swift将成为iOS开发的必然,赶紧学习下,要不落伍了。上代码吧


1.pic.jpg
import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{

var tableView:UITableView?
var dataSource = ["北京","上海","深圳","天津","河北","湖北","江苏","浙江","武汉"];


override func viewDidLoad() {
    super.viewDidLoad()
    
    self.makeTableView()
}

    func makeTableView(){
        self.tableView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.Plain)
        self.tableView!.dataSource = self;
        self.tableView!.delegate = self;
        self.view.addSubview(self.tableView!)
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identifier = "cell"
        let cell = UITableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: identifier)
    
        cell.textLabel?.text = dataSource[indexPath.row]
    
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated:true)
        let str = dataSource[indexPath.row]
        print(str)
    }
}

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务