IOS开发基础06(UITableView)

//  ViewController.swift


import UIKit


class ViewController: UIViewController {


//    let names = ["","","","",""]

    

    let keys = ["F","G","W","Q","Z"]

    let classmateArray:[[String]] = [["方亮","方黑"],["郭黄婷","桂程鹏"],["吴镇翦","王莉慧"],["邱青苗","邱宗旺"],["钟伟初","周旭凯","周杰"]]

    

    overridefunc viewDidLoad() {

        super.viewDidLoad()


        navigationItem.title ="通讯录"

        

        let rightBtn =UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Add, target:self, action: "")

        navigationItem.rightBarButtonItem = rightBtn

        

        //style:(1) .Plain 分区之间没有间距 (2) .Group 分区之间有间距

        let tableView =UITableView(frame: self.view.bounds, style:UITableViewStyle.Plain)

        //提供视图相关操作

        tableView.delegate =self

        //设置数据源代理(负责提供数据)

        tableView.dataSource =self

        view.addSubview(tableView)

        //tableView注册cell,当有cell滑出屏幕的时候会将单元格cell放到缓存池中并且给上重用标识符cell

        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell")

        

    }


    overridefunc didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }



}


//遵循多个协议用“,”隔开

extension ViewController:UITableViewDelegate,UITableViewDataSource {

    //返回每个分区的行数

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

        return classmateArray[section].count

    }

    //返回每个单元格,单元格:UITableViewCellNSIndexPath是存储该单元格是第几分区(section)第几行(row)

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

        //每一个单元格对应着一个UITableViewCell,其中封装了三个属性:imageViewtextLabledetailTextLabel

//        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")

        //tableView根据重用标识符“cell”到缓存池中查找有没有缓存的cell,有的话取出来使用,没有的话创建

        let cell = tableView.dequeueReusableCellWithIdentifier("cell")as! UITableViewCell

        

        //标题视图textLable

        //先获取整个分区的数据

        let array =classmateArray[indexPath.section]

        //再根据该分区第几行(indexPath.row)来获取具体那条数据

        let value = array[indexPath.row]

        cell.textLabel?.text = value

        //副标题视图detailTextLabel

        cell.detailTextLabel?.text ="178***"

        //图片视图imageView

        cell.imageView?.image =UIImage(named: "123321.jpg")

        return cell

    }

    //返回分区个数

    func numberOfSectionsInTableView(tableView:UITableView) -> Int {

        returnkeys.count

    }

    //返回区头标题

    func tableView(tableView:UITableView, titleForHeaderInSection section:Int) -> String? {

        returnkeys[section]

    }

    //返回区头索引

    func sectionIndexTitlesForTableView(tableView:UITableView) -> [AnyObject]! {

        returnkeys

    }

    //返回分区行高

    func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath) -> CGFloat {

        if indexPath.section ==0 {

            return50

        } elseif indexPath.section ==1 {

            return80

        } else {

            return150

        }

    }

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值