美文网首页
swift-TableView初体验

swift-TableView初体验

作者: Me小酥酥 | 来源:发表于2016-07-14 14:23 被阅读335次

尝试下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)
    }
}

相关文章

网友评论

      本文标题:swift-TableView初体验

      本文链接:https://www.haomeiwen.com/subject/zavejttx.html