IOS学习随笔二

IOS tableViewController
tableViewController ---带有Controller的类都是标准的MVC模式
tableView的使用:----tableViewController的子类,只有View
tableView和他的类型:tableView控件可以通过自己写继承于tableview的子类来控制tableview,只要这个tableview关联到这个控件,在storyboard中去设置,
关于tableview可以查看其他笔记
下面的代码是在写tableview时候需要写方法import UIKit//UITableViewDataSource为TableView提供数据的相关操作,UITableViewDelegate和列表进行交互,delegate主要是用于数据的显示 class MyTV: UITableView , UITableViewDataSource ,UITableViewDelegate{
    let TAG_LABEL = 1
    var data : NSDictionary!
    //这一段是从storyboard初始化需要加入这段.......为必须要的
    required init(coder aDecoder: NSCoder) {
        super.init(coder : aDecoder)
        data = NSDictionary(contentsOfURL: NSBundle.mainBundle().URLForResource("data", withExtension: "plist")!)  //bundle,用文件初始化一个变量,这个初始化是通过自己写的datasource文件名
        self.dataSource = self ;//自己为自己提供数据
        self.delegate = self    //这个也需要在加载的时候初始化
        
    }
    //返回section的个数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return data.count //这个data是section层面的
    }    
    //特定行获取UITableViewCell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //这个参数cell是tableViewCell指定的reuse id ,这个cell中也有一个TAG设置为1的label,函数Returns a reusable table-view cell object located by its identifier.
        var cell: AnyObject! = tableView.dequeueReusableCellWithIdentifier("cell")
        var label = cell.viewWithTag(TAG_LABEL) as UILabel
//这里先按section把值存到array里面,在对应把section中的行值转化为string
label.text! = ((data.allValues[indexPath.section]).objectAtIndex(indexPath.row) as String) return cell as UITableViewCell } //seciton头标题 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return data.allKeys[section] as? String } //section的中row的个数 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return (data.allValues[section]).count //对应section的的所有值的行数目 }
//单个cell被选择后的处理事件 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ println("\(data.allValues[indexPath.section].objectAtIndex(indexPath.row)) Clicked ") } }

IOS的seciton

 

转载于:https://www.cnblogs.com/KyleRuan/p/4296070.html

猜你喜欢

转载自blog.csdn.net/weixin_34068198/article/details/93435601