您的位置:首页 > 产品设计 > UI/UE

iOS开发UI篇—UITableview控件简单介绍

2014-11-12 14:51 232 查看
iOS开发UI篇—UITableview控件简单介绍

一、基本介绍

在众多移动应⽤用中,能看到各式各样的表格数据 。

在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支持垂直滚动,⽽且性能极佳 。

UITableview有分组和不分组两种样式,可以在storyboard或者是用代码设置。

#import "NJViewController.h"
#import "NJCarGroup.h"

@interface NJViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
*  保存所有组的数据(其中每一元素都是一个模型对象)
*/
@property (nonatomic, strong) NSArray *carGroups;
@end

@implementation NJViewController

#pragma mark - 懒加载
- (NSArray *)carGroups
{
if (_carGroups == nil) {
// 1.创建模型
NJCarGroup *cg1 = [[NJCarGroup alloc] init];
cg1.title = @"德系品牌";
cg1.desc = @"高端大气上档次";
cg1.cars = @[@"奥迪", @"宝马"];

NJCarGroup *cg2 = [[NJCarGroup alloc] init];
cg2.title = @"日韩品牌";
cg2.desc = @"还不错";
cg2.cars = @[@"本田", @"丰田", @"小田田"];

NJCarGroup *cg3 = [[NJCarGroup alloc] init];
cg3.title = @"欧美品牌";
cg3.desc = @"价格昂贵";
cg3.cars = @[@"劳斯莱斯", @"布加迪", @"小米"];
// 2.将模型添加到数组中
_carGroups = @[cg1, cg2, cg3];
}
// 3.返回数组
return _carGroups;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// 设置tableView的数据源
self.tableView.dataSource = self;

}

#pragma mark - UITableViewDataSource
/**
*  1.告诉tableview一共有多少组
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"numberOfSectionsInTableView");
return self.carGroups.count;
}
/**
*  2.第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numberOfRowsInSection %d", section);
// 1.取出对应的组模型
NJCarGroup *g = self.carGroups[section];
// 2.返回对应组的行数
return g.cars.count;
}
/**
*  3.告知系统每一行显示什么内容
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row);
//    indexPath.section; // 第几组
//    indexPath.row; // 第几行
// 1.创建cell
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

// 2.设置数据
// cell.textLabel.text = @"嗨喽";
// 2.1取出对应组的模型
NJCarGroup *g = self.carGroups[indexPath.section];
// 2.2取出对应行的数据
NSString *name = g.cars[indexPath.row];
// 2.3设置cell要显示的数据
cell.textLabel.text = name;
// 3.返回要显示的控件
return cell;

}
/**
*  第section组头部显示什么标题
*
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// return @"标题";
// 1.取出对应的组模型
NJCarGroup *g = self.carGroups[section];
return g.title;
}
/**
*  第section组底部显示什么标题
*
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
// return @"标题";
// 1.取出对应的组模型
NJCarGroup *g = self.carGroups[section];
return g.desc;
}
@end


View Code
实现效果:



提示:请自行体会把数据独立出来单独处理,作为数据模型的好处。另外,把什么抽成一个模型,一定要弄清楚。

四、补充点

contentView下默认有3个⼦视图

第2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问)

第3个是UIImageView(通过UITableViewCell的imageView属性访问)

UITableViewCell还有⼀个UITableViewCellStyle属性,⽤于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: