swift学习笔记2

倒腾一天的tableView

首先是两种方式创建tableView,控件拖拽和代码生成。

然后最开始我想做的是页面跳转进入到一个tableView,并且不使用Navigation。

目前实现必须依托于storyboard,将主页和tableView都分别与stroryboard中的scene进行绑定,但是不使用拖拽控件。

在代码中生成TableView

并且需要实现UITableViewDelegate,UITableViewDataSource。

其中datasource中必须实现

1.func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{}

2.func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{}


每个点击时的cell的计数应该是indexPath.row()


代码备案:

class SecondViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
    
    var table: UITableView?
    var datas: NSString[] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        initViews()
        loadData()
    }
    
    func initViews(){
        
        table = UITableView(frame: self.view!.frame)
        self.table!.delegate = self
        self.table!.dataSource = self
        self.table!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.view?.addSubview(self.table)
        
    }
    
    func loadData(){
        for i in 1..10{
            datas.append("str \(i)")
        }
        self.table!.reloadData()
    }
    
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
        return self.datas.count
    }
    
    
    
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
        var cell = table!.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel.text = self.datas[indexPath.row]
        return cell
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
        var i = indexPath.row
        var alert = UIAlertView()
        alert.title = "cell \(i)"
        alert.delegate = self
        alert.addButtonWithTitle("ok")
        alert.message = "you select \(datas[i])"
        alert.show()

//        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值