// 设置tableView的行高
self.tableView.rowHeight =100;
// 设置tableView分割线的样式
// UITableViewCellSeparatorStyleNone不显示分割线
// UITableViewCellSeparatorStyleSingleLine 显示分割线(默认)
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.tableView.allowsSelection=NO;//不允许选中
// 设置分割线的颜色
self.tableView.separatorColor = [UIColor orangeColor];
// 让没有内容单元格不显示[小技巧]
self.tableView.tableFooterView= [[UIViewalloc]init];
// 设置(top,left,bottom,right)[top与bottom无效]
self.tableView.separatorInset = UIEdgeInsetsMake(0,10,0,10);
在stroyborud 加载cell
CZFriendCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"friend"];
returncell;
dataSource 常用方法
/**
* 多少个分组 numberOfSectionsInTableView
*/
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return2;
}
/**
* 一个分组有多少行 numberOfRowsInSection
*/
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return2;
}
/**
* 每一个分组显示什么内容 cellForRowAtIndexPath
*/
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1创建Cell
staticNSString*reusedId =@"item";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId forIndexPath:indexPath];--------->wrong!!!!!
UITableViewCell*cell =[ tableViewdequeueReusableCellWithIdentifier:reusedId ];
if(cell ==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reusedId];
}
// 2.获取数据
LYGroup*group =_groups[indexPath.section];
LYItem*item = group.items[indexPath.row];
cell.textLabel.text=item.title;
cell.imageView.image=[UIImageimageNamed:item.icon];
//3.返回cell
returncell;
}
/**
* 分组(头部)标题 titleForHeaderInSection
*/
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @“头部”;
}
/**
* 分组(尾部)描述 titleForFooterInSection
*/
- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @“尾部”;
}
/**
* 分组索引 sectionIndexTitlesForTableView
*/
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
//返回groups数组中,所有group对象的title属性,返回的是数组
return[self.carGroups valueForKeyPath:@"title"];
}
代理常用方法
#PRagma mark -代理方法
/**
* 设置每一行的行高 heightForRowAtIndexPath
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row %2==0) {
return60;
}else{
return100;
}
}
/**
* 已经选中某一行 didSelectRowAtIndexPath
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// zd long, long long ,int
NSLog(@"用户选中了第%zd组,第%zd行",indexPath.section,indexPath.row);
}
//当取消选中某一行时候执行 didDeselectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"用户取消选中了第%zd组,第%zd行",indexPath.section,indexPath.row);
}
CELL 常用属性
// UITableViewCell *cell = [[UITableViewCell alloc] init];
// UITableViewCellStyleDefault只显示图标和名称
// UITableViewCellStyleSubtitle显示图标,名称,描述<下面>
// UITableViewCellStyleValue1显示图标,名称,描述<后面>
// UITableViewCellStyleValue2显示名称,描述<后面>
// 背景视图color
UIView *backView = [[UIView alloc] init];
backView.backgroundColor = [UIColor redColor];
// backgroundView的优先级高于backgroundColor
cell.backgroundView = backView;
// 背景颜色
cell.backgroundColor = [UIColor blueColor];
UIView *selectedView = [[UIView alloc] init];
selectedView.backgroundColor = [UIColor blueColor];
// 选中背景视图
cell.selectedBackgroundView = selectedView;
// 指示器相关
// 设置指示器的类型
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 指示器视图
cell.accessoryView = [[UISwitch alloc] init];
/**
* 从缓存中取cell //1创建Cell// 2.获取数据//3.返回cell
*/
//定义重用标识
static NSString *reuseId =@"heroCell";
//去缓冲池中查找重用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
// 如果没有找到可以重用cell就创建新的cell
if(cell ==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
// NSLog(@"创建cell");
}
// 用来刷新指定的行
[self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];
// 刷新表格
[self.tableViewreloadData];
// 滚动让某个区域可见
[self.tableViewscrollRectToVisible:(CGRect)rectanimated:YES];
//滚动到哪一行
NSIndexPath*indexPath = [NSIndexPathindexPathForRow:self.tgs.count-1inSection:0];
[self.tableViewscrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionMiddle)animated:YES];
// 第一组和最顶部的间距 headerView
self.tableView.tableHeaderView= [[UIViewalloc]initWithFrame:CGRectMake(0,0,0,15)];
// 设置 组间距15
self.tableView.sectionHeaderHeight=15;
self.tableView.sectionFooterHeight=0;
//设置tableView的背景图片
self.tableView.backgroundView= [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"bg"]];
//重写init方法,设置分组
- (instancetype)init{ return[superinitWithStyle:UITableViewStyleGrouped];
UITableViewHeaderFooterView
重写 [[selfalloc]initWithReuseIdentifier:reuseId];
UITableViewCell
重写 [[selfalloc]initWithStyle:(UITableViewCellStyleDefault)reuseIdentifier:ID];