iOS,UITableView详细介绍之基本使用(一)
来源:互联网 发布:数组转化成json 编辑:程序博客网 时间:2024/06/05 05:00
iOS中,UITableVIew的使用是非常常见的,下面就来详细介绍一下
1.UITableView必须实现两个代理:UITableVIewDataSource,UITableViewDelegate
2.UITableView的三个关键代理方法是:
(1)-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
(2)-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(3)-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
3.下面以一个最基本的例子来详细介绍UITableVIew的使用
代码如下:
#import "ViewController.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,strong)UITableView *tableView;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; //创建一个UITableView UITableView *tableView=[[UITableView alloc]init]; tableView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); //设置代理方法 tableView.delegate=self; //设置数据源 tableView.dataSource=self; self.tableView=tableView; [self.view addSubview:tableView]; //如果不想要系统自带的下划线 //self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; }//有几个部分-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}//每一部分有几条数据-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 10;}//每一数据的具体显示-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //设定一个标志,用于cell的重用的,滑出视觉范围的cell会被放入空闲队列而不是被销毁,提高性能了。 static NSString *cellId=@"cellId"; //从空闲队列里取出一个cell UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId]; //如果为空,就新建 if(!cell) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId]; } //cell的一些具体展示 cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row+1]; cell.imageView.image=[UIImage imageNamed:@"111"]; cell.detailTextLabel.text=@"小悦悦"; //如果想要系统自带的箭头 cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; return cell;}//设置每一行cell的高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60;}//每一行点击事件-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"====%ld",indexPath.row);}//设置header文字- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @"生活";}
4.效果图:
0 0
- iOS,UITableView详细介绍之基本使用(一)
- iOS:UITableView 使用(一)--基本使用
- IOS学习笔记(十一)之IOS开发之表视图(UITableView)的基本介绍(一)
- IOS学习笔记(十一)之IOS开发之表视图(UITableView)的基本介绍(一)
- IOS学习笔记(十一)之IOS开发之表视图(UITableView)的基本介绍(一)
- UITableView基本介绍(一)
- UITableView(一)----基本介绍
- IOS UITableView详细使用
- iOS -- UITableView基本使用
- iOS -- UITableView基本使用
- iOS,UITableView详细介绍之自定义Cell(二)
- iOS 初学详细之 UITableView
- UITableView介绍 之 基本用法
- iOS--UITableView的详细使用
- ios基本控件之UITableView
- ios控件 UITableView 基本使用
- iOS UITableView的基本使用
- IOS之UITableView使用
- 点击修改按钮,将alv某列的不可编辑状态转为可编辑状态
- Python编程(一):Python中tuple和list的一点思考
- OpenGL入门学习(一)
- Intent详解
- linux笔记
- iOS,UITableView详细介绍之基本使用(一)
- viewpager增加滑动属性
- C,C++宏中#与##的讲解
- 【Educational Codeforces Round 3 C】【贪心 排序】Load Balancing 最小操作步数使得数字之差尽可能小
- C语言undefined behaviour未定义行为
- iOS在Xcode中,文件名后小方框中的A(M或C)代表的意思
- Javascript实现图片预加载【回调函数,多张图片】
- 微信开放平台开发(2) 微信登录
- java面试题100道(81-100)