Swift-UITableView初识

作者: FlyElephant | 来源:发表于2016-06-06 11:00 被阅读109次

目前有些公司已经在开始转Swift,当然想要所有的公司完全过渡到Swift,还需要两年到三年的时间,有的时候要照顾公司项目的开发进度也要照顾公司团队成员的学习能力和水平,最近看了一下Swift语言,简单的从大家最常见的UITableView入手, 其实上手还是挺简单的,先来看一下效果图:

FlyElephant.png

初始化数据

    self.view.backgroundColor = UIColor.whiteColor()
    let tableFrame=CGRectMake(0, 64, UIScreen.mainScreen().bounds.width, 300)
    self.tableView = UITableView.init(frame: tableFrame, style: UITableViewStyle.Plain)
    self.tableView?.delegate=self
    self.tableView?.dataSource=self
    self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: CellIdentifier)
    self.view.addSubview(self.tableView!)
    
    self.data=["中山郎","FlyElephant","http://www.jianshu.com/users/24da48b2ddb3/latest_articles","QQ群:228407086"]

实现UITableViewDataSource

<pre><code>`
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

var tableView :UITableView?

var data=[String]()`</code></pre>

UITableViewDataSource:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let tableViewCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)
    tableViewCell?.textLabel?.text="\(self.data[indexPath.row])"
    return tableViewCell!
}

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

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

写法比较简单,实现起来也比较容易,如果有Swift问题欢迎在QQ群
228407086共同讨论~

相关文章

网友评论

    本文标题:Swift-UITableView初识

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