表格视图是IOS开发中的重要视图控件,是一个可滚动的界面(垂直滚动的UIScrollView),展示了一系列长方形的单元格(UITableViewCell,UIView的子类),这是使iPhone的小屏幕非常有用的强大的基础。表格视图具有三个目的:
1.展示信息;
2.选择;
3.导航;
UITableView有plain和group两种;
下面是简单地UITableView的练习
//
// ViewController.m
// 0924UITableView01
//
// Created by weib on 15-9-24.
// Copyright (c) 2015年 weib. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource>
//设置tableView属性
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//一:设置数据源
self.tableView.dataSource = self;//控制器就是数据源
}
//二:返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
//三:返回每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
//四;设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//1.单元格实例化
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
//2.设置单元格内容
//设置单元格标题
// cell.textLabel.text = @"hello";
//2.重新设置单元格内容
//indexPath.section第几组
//indexPath.row第几行
if ( 0 == indexPath.section) {//第0组
if (0 == indexPath.row) {//第0行
cell.textLabel.text = @"奥迪";
}
else if (1 == indexPath.row) {//第1行
cell.textLabel.text = @"宝马";
}
else if (2 == indexPath.row) {//第2行
cell.textLabel.text = @"奔驰";
}
}
else if (1 == indexPath.section) {//第1组
if (0 == indexPath.row) {//第0行
cell.textLabel.text = @"本田";
}
else if (1 == indexPath.row) {//第1行
cell.textLabel.text = @"丰田";
}
else if (2 == indexPath.row) {//第2行
cell.textLabel.text = @"三菱";
}
}
//3.返回单元格
return cell;
}
//设置每组的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (0 == section) {
return @"德系品牌";
}
else {
return @"日系品牌";
}
}
//设置每组的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if (0 == section) {
return @"产自德国,品质保证";
}
else {
return @"产自日本,比天朝的牛";
}
}
@end