Swift-UITableView的基本使用


var myTableView : UITableView?

    var dataArray : NSMutableArray?

    var sectionTitleArray : NSArray?


//创建添加表格视图

    func createTableView() {

        

        //创建myTableView

        self.myTableView = UITableView.init(frame: CGRectMake(0, 64, self.view.frame.size.width,self.view.frame.size.height-64), style: UITableViewStyle.Plain)

        

        //myTableView的背景色

        myTableView!.backgroundColor = UIColor.redColor()

        

        //成为方法代理

        myTableView!.delegate = self

        

        //成为数据源代理

        myTableView!.dataSource = self;

        

        //不显示垂直方向的进度条

        myTableView!.showsVerticalScrollIndicator = false

        

        //添加表格视图

        self.view.addSubview(myTableView!)

    }

    

    //创建数据源

    func createDdataArray() {

        

        //初始化数据源

        self.dataArray = NSMutableArray()

        

        let firstArray = NSMutableArray()

        //用循环添加数据

        for i in 0..<10 {

            

            //创建字典

            let dic:Dictionary<String,String>=["title":" (\(i)) 条数据的标题","detail":" (\(i)) 条数据的详情","image":"image(\(i))"]

            

            //把创建好的字典添加

            firstArray.addObject(dic)

        }

        

        let twoArray = NSMutableArray()

        //用循环添加数据

        for i in 0..<10 {

            

            //创建字典

            let dic:Dictionary<String,String>=["title":" (\(i)) 条数据的标题","detail":" (\(i)) 条数据的详情","image":"image(\(i))"]

            

            //把创建好的字典添加

            twoArray.addObject(dic)

        }

        

        //添加数据源

        self.dataArray?.addObject(firstArray)

        self.dataArray?.addObject(twoArray)

    }


//返回表格视图应该显示的数据的段数

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        

        return dataArray!.count

    }

    

    //返回表格视图上每段应该显示的数据的行数

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        

        return dataArray!.objectAtIndex(section).count

    }

    

    

    //返回某行上应该显示的单元格

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        

        //定义一个cell标示符,用以区分不同的cell

        let cellID : String = "cell"

        

        //cell复用池中拿到可用的cell

        /*

         我们为什么要实现单元格的复用机制?

         单元格每一个cell的生成都是要init alloc的,所以当我们滑动表格试图的时候会生成很多cell,无异于浪费了大量的内存

         单元格的复用机制原理

         一开始的时候我们创建了桌面最多能显示的单元格数,cell

         当我们向下滚动表格试图的时候,单元格上部的内容会消失,下部的内容会出现,这个时候我们将上部分消失的单元格赋给下部分出现的单元格

         因此我们就做到了只生成了屏幕范围可显示的单元格个数,就实现滑动表格试图时,以后不会再init alloc单元格cell了,从而实现了节省内存的原理

         */

        var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell!

        

        //检测,拿到一个可用的cell

        if(cell == nil)

        {

            //创建新的cell

            /*

             UITableViewCellStyle.Default,    // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)

             UITableViewCellStyle.Value1,        // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)

             UITableViewCellStyle.Value2,        // 左侧依次显示textLabel(默认蓝色)detailTextLabelimageView可选(显示在最左边)

             UITableViewCellStyle.Subtitle    // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)

             */

            

            cell = UITableViewCell.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellID)

            

        }

        

        let array =  self.dataArray!.objectAtIndex(indexPath.section)

        let dic = array.objectAtIndex(indexPath.row)

        

//        //cell上文本

        cell.textLabel?.textColor = UIColor.blackColor()

        cell.textLabel?.text = dic.objectForKey("title") as! String

//

//        //cell上的子标题

        cell.detailTextLabel?.text = dic.objectForKey("detail") as! String

        

        //cell上图片

        cell.imageView?.image = UIImage(named: dic.objectForKey("image") as! String)

        

        return cell;

    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值