iOS-UITableView基础用法篇-geekband

UITableView基本使用方法

如果自学的人可以按照这个路径去学习


iOS-UITableView基础用法篇-geekband_第1张图片
学习地图

进入要点

1.首先Controller需要实现两个delegate分别是UITableViewDelegate 和UITableViewDataSource。

 @interface ViewController : UIViewController 

2.然后 UITableView对象的 delegate要设置为 self。

3.然后就可以实现这些delegate的一些方法拉
以下是新建文件必备3代理方法,不然会出错

 //在ViewController.m文件下
 //行数
 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
return 10;//返回的数值是数组的个数
 }
 //cell
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     return [[UITableViewCell alloc]init];
 }
 //行高
 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}

那我们进入做一个小例子
1.新建项目.
在main.storyboard 拖入.tableview 并拖向viewcontroller并指向datasource 和 delegate


iOS-UITableView基础用法篇-geekband_第2张图片
Snip20160321_9.png

2.Controller需要实现两个delegate.(上文有说道)

3.插入行数组数

 //Row行数
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ( section == 0) {
    return 3;
} else {
    return 10;
 }
 }
 //Sections组数数量
 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//组别行高色
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footer = [[UIView alloc]init];
footer.backgroundColor = [UIColor redColor];
return footer;
}
//组别行底色
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *header = [[UIView alloc]init];
header.backgroundColor = [UIColor greenColor];
return header;
}

运行如下图
同时用鼠标点击 拖动一下 感受一下效果


iOS-UITableView基础用法篇-geekband_第3张图片
行数组数+组数上行颜色及下行颜色.png

4加入cell数据(行里面的数据)

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * cellID = [NSString stringWithFormat:@"cell_sec_%ld", (long)indexPath.section ];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"section %ld - row %ld",(long)indexPath.section,(long)indexPath.row];
if (indexPath == 0 ) {
    cell.imageView.image = [UIImage imageNamed:@""];//组别头像
}else{
    cell.imageView.image = [UIImage imageNamed:@""];//组别2.3头像
} 
return cell;
}

运行如下


iOS-UITableView基础用法篇-geekband_第4张图片
加入cell数据

页面做好了 现在欠缺 点击进入下一个页面.
新建页面,NextViewController
在NextViewController.h

@property (copy, nonatomic) NSIndexPath * indexPath;

在NextViewController.m
加入一个tabel(自行添加,我用的是storyboard)并添加一下代码
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.nextLabel.text = [NSString stringWithFormat:@"Selected %d.%d", (int)self.indexPath.section, (int)self.indexPath.row ];
}

在storyboard 建立一个segue指向NextViewController.m


iOS-UITableView基础用法篇-geekband_第5张图片
指向

segue名字修改为next

回到viewcontroller.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"showDetail" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ( [segue.identifier isEqualToString:@"showDetail"] ) {
DetailedViewController * dvc = segue.destinationViewController;
dvc.indexPath = sender;
}

运行一下.


iOS-UITableView基础用法篇-geekband_第6张图片

你可能感兴趣的:(iOS-UITableView基础用法篇-geekband)