您的位置:首页 > 产品设计 > UI/UE

UITableView的使用

2015-09-11 01:34 477 查看
UITableView的使用 今天凌晨,苹果召开2015年新品发布会。iPhone6s和iPhone6s Plus,以及iPad Pro、新Apple TV会与我们见面,据悉,iPhone 6s与iPhone6s Plus将会在9月18日正式发售。



表格视图控制器,它是一个是视图控制器的对象管理一个表视图。
一个iphone表格由三种东西构成:一个视图,一个数据源和一个委托。
数据源:它是用于管理可见的UITableview及其数据之间关系-UITableviewDataSource协议中的方法提供表格的区域,行数,顶部和底部的标题,以及每个单元格的内容等。其中有两个方法必须实现:tableView:numberOfRowsInSection:和tableView:cellforRowAtIndexPath:
委托:控制表格的视觉外观和行为-UITableviewDelegate协议的对象会收到从多用户活动的通知,如选择一个单元格,编辑单元格等操作。通常我们需要实现tableView:didSelectRowAtIndexpath:

数据源方法:
//不重写默认返回1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//在tableview一个区域显示多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataSourceArray count];
}
//填充数据
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// addMovies = [[Movie alloc]init];
// addMovies.movieName = avcc.Addmovies.movieName;
// addMovies.movieMoney = avcc.Addmovies.movieMoney;
// addMovies.movieDescription = avcc.Addmovies.movieDescription;
// [dataSourceArray addObject:addMovies];
static NSString* Cellidentifer = @"cell";
//cell的重用
// UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];
//系统
// addMovies.movieName =
MyCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];
//自定义
if (cell==nil) {
//初始化一个cell
//这里的style是一个枚举
cell = [[MyCell alloc]initWithStyle:0 reuseIdentifier:Cellidentifer];
//这里是在最后面加一个->
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

NSLog(@"%@",[dataSourceArray objectAtIndex:indexPath.row]);
// cell.textLabel.text = [dataSourceArray objectAtIndex:indexPath.row];
Movie* movie = [dataSourceArray objectAtIndex:[indexPath row]];
// NSString* strMoney = [NSString stringWithFormat:@"%d",movie.movieMoney];
NSLog(@"%d",[indexPath row]);
NSLog(@"%@",[dataSourceArray objectAtIndex:[indexPath row]]);
cell.Movieimage.image = [UIImage imageNamed:movie.movieImage];
cell.MovieMoney.text = movie.movieMoney;
cell.MovieName.text = movie.movieName;
cell.MovieDescription.text = movie.movieDescription;

return cell;
}

单元格重用机制
//填充数据
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// addMovies = [[Movie alloc]init];
// addMovies.movieName = avcc.Addmovies.movieName;
// addMovies.movieMoney = avcc.Addmovies.movieMoney;
// addMovies.movieDescription = avcc.Addmovies.movieDescription;
// [dataSourceArray addObject:addMovies];
static NSString* Cellidentifer = @"cell";
//cell的重用
// UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];
//系统
// addMovies.movieName =
MyCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];
//自定义
if (cell==nil) {
//初始化一个cell
//这里的style是一个枚举
cell = [[MyCell alloc]initWithStyle:0 reuseIdentifier:Cellidentifer];
//这里是在最后面加一个->
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

NSLog(@"%@",[dataSourceArray objectAtIndex:indexPath.row]);
// cell.textLabel.text = [dataSourceArray objectAtIndex:indexPath.row];
Movie* movie = [dataSourceArray objectAtIndex:[indexPath row]];
// NSString* strMoney = [NSString stringWithFormat:@"%d",movie.movieMoney];
NSLog(@"%d",[indexPath row]);
NSLog(@"%@",[dataSourceArray objectAtIndex:[indexPath row]]);
cell.Movieimage.image = [UIImage imageNamed:movie.movieImage];
cell.MovieMoney.text = movie.movieMoney;
cell.MovieName.text = movie.movieName;
cell.MovieDescription.text = movie.movieDescription;

return cell;
}
UITableView中显示的每一个单元都是一个UITableview对象,它的初始化函数initWithStyle:reuseIdentifier:比较特别,跟我们平时看到的UIView的初始化函数不同。这个主要是为了效率考虑,因为在tableview快速滑动的过程中,频繁的alloc对象是比较费时的,于是引入cell的重用机制,这个也是我们在打他source中要重点注意的地方,用好重用机制会让我们的tableView滑动起来更加流畅。

static NSString* Cellidentifer = @"cell";
定义一个静态字符串常量,用于指定cell的标示符.
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];
从表格视图中获取带标示符的空闲(未显示)的cell,如果有则获得cell,否则cell为nil;
if (cell==nil) {
//初始化一个cell
//这里的style是一个枚举
cell = [[MyCell alloc]initWithStyle:0 reuseIdentifier:Cellidentifer];
//这里是在最后面加一个->
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
如果从表格视图中获取不到可用的cell,则alloc出一个新的cell,并设置标示符。

UITableViewCell
单元格显示类型:
typedef enum{
UITableViewStyleDefault; à0
UITableViewCellStyleValue1; à1
UITableViewcellStyleValue2; à2
UITableViewCellStyleSubtitle; à3
}








单元格的辅助图标类型
//这里是在最后面加一个->
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;








编辑单元格(增,删,移动)
1. 设置tableview的编辑模式:
















选中单元格—>进入二级视图进行修改信息








更新单元格内容








需要先引入协议








分段按钮实现














自定义单元格






























本文出自 “wodenianshao的技术博客” 博客,请务必保留此出处http://wodenianshao.blog.51cto.com/10604330/1693680
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: