VB
UITabelView 是iOS中用来以列表形式展开以及编辑信息的控件,集成自UIScrollView 所以能滑动,因为只有一列,所以只能在垂直方向滑动
UITabelView由分区section(班级内的分组)和分区内的行(分组内的人)组成.而且索引都是从零开始,要取到某一行,必须通过分区索引以及行索引,每一行的位置统一用一个NSIndexPath的类型的对象存储
UITabelView 的样式创建之后不可改变
属性有 datasource数据源,用来用tabelView提供数据
设置分割线的颜色 separatorColor
设置分割线样式 separatorStyle
设置分区索引的颜色 sectionIndexColor
设置tabelView页眉 tabelHeaderView
#pragma mark - UITableViewDataSource
//设置UITableView的分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.stuDic count];
}
//为tableView设置对应分区中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_stuDic[_sortedKeys[section]] count];
}
//tableView的每一行都对应一个UItableViewCell对象
//针对于每一行返回一个UItableViewCell对象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//1.创建重用标识符
static NSString *identifier = @"acer";
//2.去重用队列(仓库)中根据重用标志符去取可重用的cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//3.判断重用队列中是否成功取到可重用的cell.
if (!cell) {
//cell 为空 说明没有取到可重用的cell,就创建一个新的cell对象
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
NSLog(@"123");
}
//设置赋值视图的样式
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text = _stuDic[_sortedKeys [indexPath.section]][indexPath.row];
return cell;
}
//设置tableView分区的页眉title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return _sortedKeys[section];
}
//设置tableView分页的页脚title
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
// return @"Duck";
//}
//设置tableView右边缘的分区索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return _sortedKeys;
}
#pragma mark - UITableViewDelegate
//当选中cell时触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"section:%ld, row:%ld", (long)indexPath.section, (long)indexPath.row);
}
//取消选中时触发(前提是已经选中了一个cell)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
NSLog(@"触发");
//indexPath 为取消选中的cell的位置
NSLog(@"deselect section:%ld, row:%ld", (long)indexPath.section, (long)indexPath.row);
}
//当点击cell上的辅助视图按钮时的触发
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSLog(@"hahahha");
}
//设置tableView每一行的高度,该方法在创建cell之前
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2) {
return 50;
}
return 50;
}
//设置tableView分区页眉的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50;
}
//设置tableView分区页脚的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 50;
}