iOS控件之UITableView

iOS控件之UITableView

基础:

//这段代码在ViewController.h中
@interface ViewController:UIViewController 
<
//实现数据视图的普通协议
//数据视图的普通事件处理
UITableViewDelegate,
//实现数据视图的数据代理协议
//处理数据视图的数据代理
UITableViewDataSource>
{
//定义一个数据视图对象
//数据视图是用来显示相同格式的信息的视图
//例如:电话通讯录,QQ好友,朋友圈信息
UITableView* _tableView;

//申明一个数据源
NSMutableArray* _arrayDate;
}

//这段代码写在ViewController.m里面
- (void)viewDidLoad {
	[super viewDidLoad];

	//创建数据视图
	//P1:数据视图的位置
	//P2:数据视图的风格
	//UITableViewStylePlain:普通风格
	//UITableViewStyleGrouped:分组风格
	_tableView = [[UItableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
	//设置数据视图的代理对象
	_tableView.delegate = self;
	//设置数据视图的数据源对象
	_tableView.dataSource = self;

	//添加到当前视图
	[self.view addSubview:_tableView];

	//如果你自定义了一个UITableViewCell
	//注册用于创建新表单元格的类
	[self.tableView registerClass:[CpwTableViewCell class]  doeCellReuseIdentifier:@"myCell"];
}
//设置表格每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		return 120;
	} else if (index.Path.section == 5) {
		return 80;
	} else if (indexPath.section == 2 && index.row == 2) {
		return 150;
	}  else  { 
		return 50;
	}
}
//获取每组元素的个数(行数)
//必须要实现的协议方法
//程序在显示数据视图时会调用此方法
//返回值:表示每组元素的个数
//P1:数据视图对象本身
//P2:那一组需要的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRousInSection:(NSIntegar)section {
NSArray* array = [NSArray arrayWithObjects:@1, @2. @3, @6, @2, @1, nil];
return [array[dection] integarValue];

}

//设置数据视图的组数
//默认返回1
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 6;

}

//创建单元格对象函数
//必须实现的协议方法
//你的单元格有多少个,这个方法就会被调用几次;
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	由于此方法的调用十分繁琐,cell的标识成静态变量有利于性能优化
	
	static NSString* ID = @"cell";
	//在重用池中去找
	UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:ID];
	
	if (cell == nil) {
		//创建一个单元格对象
		//参数二:单元格的样式
		//如果找不到就创建
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
	}
	NSString* str = [NSString stringWitFormat:@"第%ld组,第%ld行!",indexPath.section, indexPath.row];

	//将单元格的主文字内容赋值
	cell.textLabel.text = str;
	cell.backfroundColor = [UIColor blueColor];
	return cell;
}

有个问题,为什么这里ID变量前面要加static呢?
首先,复习一下iOS变量作用域和生命周期的知识:
1.iOS方法中的临时变量(比如上面的ID变量)存储在栈区,出了该方法,临时变量就会被自动销毁,但是如果你加上static修饰的话,就算出了该方法也不会被销毁,它会一直存在直到程序结束才会被销毁,释放内存。
2.字符串常量(比如上面的@“cell”)和所有方法之外申明的全局变量,存储在常量区,一旦创建,直到程序结束才会被销毁。

因此,显然ID变量让其只创建一次,然后一直指向@“cell”,这就比较合理啦。
补充一点静态局部变量的知识:定义后只会存在一份值,每次调用都是使用的同一个对象内存地址的值,并没有重新创建,节省空间,只能在该局部代码块中使用。所以这样定义主要是为了节省空间。
如果还有疑问,可以参考这篇博客

好了。回到UITableView.
一些常用协议方法

//获取每组高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 100;
}
//获取每组头部标题
-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return @"头部标题";
}
//获取每组尾部标题
-(NSString*) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    
    return @"尾部标题";
}
//获取头部高度
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
//尾部高度
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 60;
}

在讲一点更高级的
在这之前,需要写一个导航控制器了

//这段代码写在Appdelegate.h里面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
    
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}

未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值