Swift-UITableView的基本使用
来源:互联网 发布:秋冬需要防晒 知乎 编辑:程序博客网 时间:2024/05/16 01:32
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 iin 0..<10 {
//创建字典
let dic:Dictionary<String,String>=["title":"第 (\(i))条数据的标题","detail":"第 (\(i))条数据的详情","image":"image(\(i))"]
//把创建好的字典添加
firstArray.addObject(dic)
}
let twoArray =NSMutableArray()
//用循环添加数据
for iin 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 {
returndataArray!.count
}
//返回表格视图上每段应该显示的数据的行数
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
returndataArray!.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(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
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;
}
- Swift-UITableView的基本使用
- swift中UITableView的基本使用(基于Storyboard):适用新手
- Swift - UITableView的基本使用与一个大坑点
- swift的UITableView的使用
- Swift之UITableView的使用
- Swift之UITableView的使用
- swift之UITableView的使用
- Swift UITableView 的简单使用
- swift-UITableView的根本使用
- UITableView的基本使用
- UITableView 的基本使用
- UITableView 的基本使用
- UITableView的基本使用一
- iOS UITableView的基本使用
- Swift UITableView 使用的简单介绍
- swift UITableView使用
- swift - UITableView 使用
- swift UITableView使用
- JavaScript中原型和原型链详解
- Java中UIManager的几种外观的详细讲解
- Hadoop1.0学习笔记之基础
- 基于jQuery倒计时插件实现团购秒杀效果
- Android学习笔记(一)大体认识
- Swift-UITableView的基本使用
- Directshow中的视频捕捉
- OpenNMS和商业开源网管软件SugarNMS的比较
- OpenGL系列教程之二:OpenGL概述
- 判断浏览器类型
- 中英文对照 —— 十二星座
- excel poi 设置列宽度
- OpenGL系列教程之三:OpenGL渲染管线
- CABAC中的二值化方法