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

UI初级第七课  表视图的使用——iOS学习连载22

2015-08-26 08:19 423 查看
1.创建一个表视图

UITableView *tableView = [[UITableView
alloc]
initWithFrame:self.view.bounds];

2.以代理对方式设置数据源,显示什么数据由代理对象来确定

tableView.dataSource
=
self;

tableView.backgroundColor
= [UIColor
greenColor];

[self.view
addSubview:tableView];

3.获取数据源

array = [UIFont
familyNames];

4.
#pragma mark -UITableViewDataSource

//当前显示的数据有多少个

- (NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section

{

return
array.count;

}

//返回每一条数据对应的单元格对象

- (UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath

{

//初始化一个单元格

UITableViewCell *cell = [[UITableViewCell
alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];

//通过indexPath拿到构建单元格的行的下标

NSLog(@"section:--%ld, row:--%ld",
indexPath.section, indexPath.row);

NSString *fonName = [array
objectAtIndex:indexPath.row];

cell.textLabel.text
= fonName;

cell.textLabel.font
= [UIFont
fontWithName:fonName
size:20];

return cell;

}

5.表视图的样式:
UITableViewStyleGrouped:分组样式

UITableViewStylePlain:
平铺样式(默认)

6.

设置tableView的内容偏移量

tableView.contentInset =
UIEdgeInsetsMake(20,
0, 0,
0);

7.

获取文件路径方法一:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"plist"];

方法二:

//拿到bundle的源路径

NSString *resourcePath = [[NSBundle
mainBundle]
resourcePath];

//文件路径

NSString *filePath = [resourcePath
stringByAppendingPathComponent:@"font.plist"];

8.

根据一个文件路径返回一个数组

self.data = [NSArray
arrayWithContentsOfFile:filePath];

9.

设置表视图分割线风格:

//UITableViewCellSeparatorStyleNone:没有分割线

_tableView.separatorStyle
=
UITableViewCellSeparatorStyleNone;

10.

设置表视图分割线颜色,默认标准灰色

_tableView.separatorColor = [UIColor
redColor];

11.
//设置单元格的行高
--默认44

_tableView.rowHeight =
100;

以上方法只能更改所有单元格的行高均为100,如果需要动态地确定行高需要在代理方法中实现

- (CGFloat)tableView:(UITableView
*)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath

{

if (indexPath.row
==
0) {

return
100;

}else
if
(indexPath.row
==
1) {

return
200;

}

return
44;

}

12.

删除第一个元素

[self.data
removeObjectAtIndex:0];

必须刷新表视图才能够删除

[self.tableView
reloadData];

13.
//把第2个单元格的内容改为“hello”

//构建IndexPath

NSIndexPath *indexPath = [NSIndexPath
indexPathForRow:2
inSection:0];

//取到单元格

UITableViewCell *cell = [self.tableView
cellForRowAtIndexPath:indexPath];

cell.textLabel.text =
@"hello";

注意:不能刷新表视图,因为刷新表视图会重新执行数据源方法和代理方法,将会重写第1个单元格的内容,即仍为原来单元格的内容

14.

输出在屏幕上显示的单元格的内容

NSArray
*cells = [self.tableViewvisibleCells];

for
(UITableViewCell
*cell
in cells) {

NSLog(@"%@",
cell.textLabel.text);

}

15.

输出在屏幕上显示的单元格的下标

NSArray
*indexPaths = [self.tableView
indexPathsForVisibleRows];

for
(NSIndexPath
*indexPath
in indexPaths) {

NSLog(@"%ld",
indexPath.row);

}

16.

滑动到指定的位置,可以配置动画

NSIndexPath
*indexPath = [NSIndexPath
indexPathForRow:10
inSection:0];

[self.tableView
scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:true];

17.

返回自定义的组的头视图(一定要实现返回组头视图高度的协议方法)

//组头视图高度的协议方法

- (CGFloat)tableView:(UITableView
*)tableView heightForHeaderInSection:(NSInteger)section

{

return
50;

}

18.

让当前视图控制器中的第一个滑动视图默认往下偏移64个像素

self.automaticallyAdjustsScrollViewInsets =
NO;

19.

排序----compare:是系统的排序方法

allKeys = [allKeys
sortedArrayUsingSelector:@selector(compare:)];

20.
//设置组索引的颜色

tableView.sectionIndexColor
= [UIColor
redColor];

//设置组索引的背景颜色

tableView.sectionIndexBackgroundColor
= [UIColor
greenColor];

//设置组索引选中后的颜色

tableView.sectionIndexTrackingBackgroundColor = [UIColor
blueColor];

21.复用单元格

- (UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath

{

//创建一个identifier

NSString *identifier =
@"Cell";

//到复用池中取一个复用单元格

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:identifier];

if (cell ==
nil) {

//如果复用池没有闲置的单元格,则创建

cell = [[[UITableViewCell
alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier]
autorelease];

}

cell.textLabel.text
=
self.data[indexPath.row];

return cell;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: