泽兴芝士网

一站式 IT 编程学习资源平台

iOS创建简单表视图UITableView

记录一个菜鸟的IOS学习之旅,如能帮助正在学习的你,亦枫不胜荣幸;如路过的大神如指教几句,亦枫感激涕淋!


细心的朋友可能已经注意到了,IOS学习之旅系列教程在本篇公众号的文章中,封面已经换成美女图片了,程序员的福利有木有!!!


这是亦枫将在微信公众号 [技术鸟] 的IOS系列文章中采取的路线。看文章,学技术,眼睛累了马上点击大图看美女,这难道就是传说中的养眼吗!!!

在iOS中使用表视图UITableView,最重要的是使用好表视图的两个协议: UITableViewDataSource 数据源协议和 UITableViewDelegate 委托协议。灵活使用这两个协议便可以自由定义表视图的内容数据和单元格样式。


在这篇文章中,我们从使用iOS系统api提供的单元格到自定义单元格样式创建简单的表视图,学习UITableView的使用,同时掌握各种其他iOS基础知识。


使用 Simple View Application 模板创建一个工程,由于这里我们使用的是表视图控制器,而系统生成的模板是普通视图控制器,所以我们需要在故事板文件的 View Controller Scene 中删除View Controller,在控件库中重新拖拽一个Table View Controller到设计界面中。





在设计界面左侧document outline窗口面板中选中View Controller,打开标识检测器,在class属性中选择ViewController,注意在属性检测器中勾选 Is Initial View Controller ,设置为默认视图控制器。再选中Table View,打开属性检测器窗口,在content属性下拉菜单中有两个选项,Dynamic Prototypes和Static Cells,即动态表和静态表,这里我们选择动态表。再选中Table View Cell,打开属性检测器,在style属性中系统提供了几种单元格样式,默认是custom样式,Identifier属性表示可重用单元格的标识符,类似于一个ID,可自由定义,这里我们设为CellIdentifier,然后在代码中获取单元格对象的时候就不需要再实例化单元格了,直接引用即可。


比如,之前我们获取单元格对象时的代码是这样的:

 NSString *identifierString = @"simpleTableViewCell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierString];
 if (cell==nil) {
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierString];
 }

在故事板文件中设置了IDentifier属性之后,代码如下:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIentifier"];

ViewController.h 文件中继承的父类ViewController替换成UITableViewController,并定义一个数据属性:

#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@property (strong, nonatomic)NSArray *simpleData;
@end

数组内容从文件Property List文件中读取。在工程文件存放代码的目录中新建一个plist文件,添加测试数据,如图所示:





在ViewController.m文件中读取plist中的数据,同时实现数据源协议的两个方法:tableView: numberOfRowsInSection:tableView: cellForRowAtIndexPath: ,分别用于设置节中的单元格行数和单元格视图,代码如下:

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"simpleData" ofType:@"plist"];
 self.simpleData = [[NSArray alloc] initWithContentsOfFile:filePath];
}
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 return [self.simpleData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 NSString *identifierString = @"simpleTableViewCell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierString];
 if (cell==nil) {
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierString];
 }
 NSInteger rowIndex = [indexPath row];
 NSDictionary *cellDictionary = [self.simpleData objectAtIndex:rowIndex];
 cell.textLabel.text = [cellDictionary objectForKey:@"name"];
 cell.imageView.image = [UIImage imageNamed:[cellDictionary objectForKey:@"cover"]];
 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
 return cell;
}
@end

运行效果如下:





注意:在iOS7之后,状态栏是透明的,所以上图中的应用界面顶部和状态栏重叠在一起,不过不用担心,通常我们的应用顶部会有导航栏或者直接隐藏状态栏,甚至可以设置应用界面距离顶部的距离,避免上图重叠引起的视觉问题。


这样,通过上述介绍就能实现iOS应用中简单的表视图效果,较为基础,在下一篇文章中我们一起学习如何自定义视图实现更为丰富的单元格样式,并为表视图添加搜索栏。

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言