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)
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,200评论 4 61
  • 转载自:https://github.com/Tim9Liu9/TimLiu-iOS[https://github...
    香橙柚子阅读 8,686评论 0 36
  • 风铃浪子第十辑飞血一剑终入魔 第一章 圈套 禚越和那姓辜的剑士本来打算向前走的,这时候止住了脚步。禚越打了个手势,...
    紫拉加一阅读 283评论 2 1
  • 【日精进打卡第24天】 姓名:赵福缘 公司:青柠养车 【知~学习】 【行~实践】 一、修身: 二、努力: 1、提高...
    夜勿忧阅读 179评论 0 0