UITableView 的基础用法

这篇博文简单介绍一下表视图的基础用法

添加tableView

1
2
3
4
5
6
7
8
9
10
- (void)viewDidLoad {
[super viewDidLoad]
//方法全名
UITableView *tableView = [ [UITableView alloc] initWithFrame : (CGRect) style : (UITableViewStyle)];
//方法使用
UITableView *tableview = [ [UITableView alloc] initWithFrame: self.view.bounds style : UITableViewStyleGroupde];
//一种是多组数据另一种是单组数据(这里是多组数据)
tableView.dateSourse = self;
[self.view addSubview : tableView];
}

为tableView添加方法

这些方法需要遵守协议:

@interface  NSObject <UITableViewDateSource>

数据源方法

1
2
3
4
//定义一共有多少组(section == 区域\组)
- (NSInteger)numberOfSectionsInTableview : (UITableView *)tableView{
return 数字;
}
1
2
3
4
 //定义第section组有多少行
- (NSInteger)tableView : (UITableview *)tableView numberOfRowsInSection : (NSInteger)section{
return 数字;
}
1
2
3
4
5
6
 //返回每一行显示的内容(每一行显示怎样的cell)
- (UITableViewCell *)tableView : (UITableView *)tableViewCellForRowAvtIndexPath : (NSIndexPath *)indexPath{
//indexPath标识唯一的一行
//即section组的第row行
UITableViewCell *cell(变量名,可以自定义) = [ [UITableViewCell alloc] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : nil(暂定为nil)];
}

使用数组为cell添加内容

首先在interface里定义数组变量

NSArray *_array(数组名);

然后在implementation里初始化数据

1
2
3
4
5
_array = @[
@[…],
@[…],
@[…]
];

然后在numberOfSectionsInTableview方法里获得section的数量

return _array.count;

再在numberOfRowsInSection方法里获取section里成员的数量

1
2
NSArray *sectionNumber = _array[section];
return sectionNumber.count;

然后在cell方法里设置文字

1
2
3
4
5
6
7
8
//取出第section组第row行的文字数据
NSString *text = _array[indexPath.section][indexPath.row];
//上式分开写为
NSArray *sectionNumber = _array[indexPath.section];
NSString *text = sectionNumber[indexPath.row];
//显示文字数据
cell.textLabel.text = text;
return cell;

为tableView添加头部尾部标题

方法名:

1
2
//头部
- (NSString *)tableView : (UITableView *)tableView titleForHeaderInSection : (NSInteger)section
1
2
//尾部
- (NSString *)tableView : (UITableView *)tableView titleForFooterInSection : (NSInteger)section

使用字典为cell添加内容

首先在interface里定义字典变量

NSDictionary *_dictionary(字典名);

然后在implementation里初始化数据

1
2
3
4
5
6
7
8
_dictionary= @[
@“Header” : @“",
@“Footer” : @“",
@“key” : @[@[…],
@[…],
@[…]
]
];

然后在numberOfSectionsInTableview方法里获得section的数量

return _dictionary.count;

再在numberOfRowsInSection方法里获取section里成员的数量

1
2
3
4
5
6
//获取第section组里的字典
NSDictionary *dictionary = _dictionary[section];
//获取字典里的key数组
NSArray *key = dictionary[@“key”];
//获得数字
return key.count;

然后在cell方法里设置文字

1
2
3
4
5
6
7
//取出第section组中的字典中的key数组里面的第row行的文字数据
NSDictionary *dictionary = _dictionary[indexPath.section];
NSArray *key = dictionary[@“key”];
NSString *text = key[indexPath.row];
//显示文字数据
cell.textLabel.text = text;
return cell;

利用字典给tableView添加头部尾部文字

1
2
3
4
5
//第一种方法
NSDictionary *dictionary = _dictionary[section];
return dictionary[@“Header"];
//第二种方法
return _dictionary[section][@“Header”];

添加索引条

1
2
3
4
5
6
7
8
	- (NSArray *)sectionIndexTitlesForTableView : (UITableView *)tableView {
NSMutableArray *mutableArray = [NSMutableArray array];
for (类 *变量 in _array){
[array addObject : 变量.header];
//用变量的头部文字作为索引对象
return 变量;
}
}

点击cell不变色

这是一个让你点击cell时不会让cell变色的小方法
1
cell.selectionStyle = UITableViewCellSelectionStyleNone;

尾声

基础用法大概就这么多,tableView还有很多强大并且神奇的方法,就请读者自己去探索了