UITableView的简单使用!
2015-11-03 13:19
525 查看
首先,在IOS的开发中表格视图扮演着重要的作用,现在市面上流行的APP大多有这个,下面我们来简单学习一下。
如果是在UIViewController里面写,那么我们可以写一个UITableView的属性;当然第一步还是要写Get的方法,如果你只是需要一个简单的表格视图,那么你完全可以用系统所提供的cell就行,如果是想自己定义表格视图上的内容,那么就需要你自己建立一个cell的类,这我们下面会讲。
方法:设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
if (_twoTableView.superview);
方法:设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (_twoTableView.superview)
方法:设置cell(这里添加内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法:单元格点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//当然上面的方法需要写协议方法,下面有代码
附上一个最简单的表格视图:
//声明属性
@property(nonatomic,strong)UITableView;
方法:设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
if (_twoTableView.superview){
return 10;//这里一般通过数据的多少来定义有多少行
}
方法:设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (_twoTableView.superview){
return 100;//每个单元格的高度
}
方法:单元格点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//这里写入单元格的点击方法
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KUITableViewCellIdentifierTwo];
cell.textLabel.text = @"输入内容";
cell.imageView.image
= [UIImage imageNamed:@""];//图片名字
cell.detailTextLabel.text = @"输入内容";//副文本
return cell;cell.
}
}
get方法
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 155, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-64-140)
style:UITableViewStylePlain];
//设置代理
_tableView.delegate = self;
//设置数据源
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
}
return _tableView;
}
*tableView;
如果是在UIViewController里面写,那么我们可以写一个UITableView的属性;当然第一步还是要写Get的方法,如果你只是需要一个简单的表格视图,那么你完全可以用系统所提供的cell就行,如果是想自己定义表格视图上的内容,那么就需要你自己建立一个cell的类,这我们下面会讲。
方法:设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
if (_twoTableView.superview);
方法:设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (_twoTableView.superview)
方法:设置cell(这里添加内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法:单元格点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//当然上面的方法需要写协议方法,下面有代码
附上一个最简单的表格视图:
//声明属性
@property(nonatomic,strong)UITableView;
方法:设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
if (_twoTableView.superview){
return 10;//这里一般通过数据的多少来定义有多少行
}
方法:设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (_twoTableView.superview){
return 100;//每个单元格的高度
}
方法:单元格点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//这里写入单元格的点击方法
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KUITableViewCellIdentifierTwo];
cell.textLabel.text = @"输入内容";
cell.imageView.image
= [UIImage imageNamed:@""];//图片名字
cell.detailTextLabel.text = @"输入内容";//副文本
return cell;cell.
}
}
get方法
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 155, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-64-140)
style:UITableViewStylePlain];
//设置代理
_tableView.delegate = self;
//设置数据源
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
}
return _tableView;
}
*tableView;
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- 分享一个iOS下实现基本绘画板功能的简单方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音