// 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
}
//返回每个单元格,单元格:UITableViewCell,NSIndexPath是存储该单元格是第几分区(section)、第几行(row)
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
//每一个单元格对应着一个UITableViewCell,其中封装了三个属性:imageView,textLable,detailTextLabel
// 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
}
}
}