//************************************************************ UITableView 03-实现数据源的方法
//1.连线ViewController.m. 中 创建tableview属性
//2.遵守数据源协议
@interface ViewController ()<UITableViewDataSource>
#pragma mark -数据源的方法
//实现数据源的方法
//返回多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carGroups.count;
}
//返回多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//获取这组的数据
CZCarGroup *carGroup = self.carGroups[section];
return carGroup.cars.count;
}
//设置cell 内容
//缓存池 cell 的重用
//中加载cell 提高了性能
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.创建可重用的cell
//从缓存池中取可用的cell
static NSString *reuseId =@"hero";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuseId ];
//判断有没有 如果没有就创建 有的话就就取出来用
if (cell==nil) {
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId ];
}
// 2 设置cell内部的子控件
// 2.1 获取当前要展示的数据
CZHero *hero = self.heroes[indexPath.row];
// 2.2 设置值
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image =[UIImage imageNamed:hero.icon];
//3 返回
return cell;
}
//分组的头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
CZCarGroup *carGroup = self.carGroups[section];
return carGroup.title;
}
//分组的尾部描述
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
CZCarGroup *carGroup = self.carGroups[section];
return carGroup.desc ;
}
//返回组的索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//kvc
return [self.carGroups valueForKeyPath:@"title"];
}
//设置行高
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
//设置tableView的cell的高度的两种方法
self.tableView.rowHeight = 60; //只能为所有cell设置相同的行高
//连线设置代理 controller遵守代理协议
#pragma mark - tableView的代理方法可以根据不同的cell分别设 置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 100; }
return 60; }
(扩展方法学习区域)
//设置 cell 的样式
//UITableViewCell *cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
(1)UITableViewCellStyleDefault 左图 右名字
(2)UITableViewCellStyleSubtitle 左图 右上名字 右下描述
(3)UITableViewCellStyleValue1 左图 中名字 右边描述
(4)UITableViewCellStyleValue2 名字 描述 没图片
//设置cell的附属物
//cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator
(1)UITableViewCellAccessoryDisclosureIndicator 向右的箭头 >
(2)UITableViewCellAccessoryCheckmark 对号 √
(3)UITableViewCellAccessoryDetailDisclosureButton 圆圈内的感叹号 !
(4)UITableViewCellAccessoryDetailButton 感叹号+箭头 !>
//设置cell的背景
cell.backgroundColor =[UIColor clearColor];//透明的背景颜色
//设置详细内容label的属性 就是把描述的所有文字都显示出来
cell.detailTextLabel.numberOfLines = 0;
- (void)viewDidLoad {
[super viewDidLoad];//在视图加载完毕的时候
//设置行高
self.tableView.rowHeight =60;
//设置连线数据源
self.tableView.dataSource =self;
//设置分割线
self.tableView.separatorInset =UIEdgeInsetsMake(100,0,100,0);
//设置分割线的颜色
self.tableView.separatorColor =[UIColor blueColor];
//设置分割线的样式
self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone ;
//自定义头部和尾部(加载更多 或者是分类的信息)
self.tableView.tableHeaderView = [UIButton buttonWithType: UIButtonTypeContactAdd]; //加号
self.tableView.tableFooterView
//设置背景颜色
self.tableView.backgroundColor=[UIColor redColor];
//设置背景图片
UIView *view =[[UIView alloc]init];
view.backgroundColor =[UIColor yellowColor];
self.tableView.backgroundView =view;
}
IOS学习之——实现数据源的方法(代码)
最新推荐文章于 2022-01-21 15:31:39 发布