IOS学习 UITableView 委托方法
2016-03-09 23:13
483 查看
#import <UIKit/UIKit.h>
#define VIEW_WIDTH self.view.bounds.size.width
#define VIEW_HEIGHT self.view.bounds.size.height
#import "DetaiViewController.h"
@interface HomeViewController :
UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
}
@property (nonatomic,retain)
NSArray *listArray;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
_listArray =
@[@"UITableViewStylePlain",@"UITableViewStyleGrouped"];
_tableView = [[UITableView
alloc]initWithFrame:self.view.bounds
style:UITableViewStylePlain];
[self.view
addSubview:_tableView];
//设置数据源
_tableView.dataSource =
self;
//设置委托
_tableView.delegate =
self;
}
#pragma mark - UITableView DataSource
//表视图中存在section的个数,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//section中包含row的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return
_listArray.count;
}
//创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIndentifier =
@"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIndentifier];
}
cell.textLabel.text =
_listArray[indexPath.row];
return cell;
}
#pragma mark - UITableView delegate
//当用户选择某一行时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetaiViewController *detaiVC = [[DetaiViewController
alloc]init];
//获取点击内容
detaiVC.isPlain = indexPath.row ?
NO : YES ;
[self.navigationController
pushViewController:detaiVC animated:YES];
}
@interface DetaiViewController :
UIViewController<UITableViewDelegate,UITableViewDataSource>{
NSArray *array;
NSMutableArray *tempArray;
NSMutableArray *fontArray;
}
@property (nonatomic,assign)
BOOL isPlain;
@end
@implementation DetaiViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
array = [UIFont
familyNames];
//根据选择内容,使用表格的类型
UITableViewStyle style =
self.isPlain ?
UITableViewStylePlain :UITableViewStyleGrouped ;
NSLog(@"%ld",(long)style);
UITableView *detaiTV = [[UITableView
alloc]initWithFrame:self.view.bounds
style:style];
[self.view
addSubview:detaiTV];
fontArray = [[NSMutableArray
alloc]init];
for (int i =
0; i<array.count; i++) {
//取出字体
NSString *font =
array[i];
//将5整除的时候,创建tempArray数据,添加至fontArray中
if (i %5 ==
0) {
tempArray = [[NSMutableArray
alloc]init];
[fontArray
addObject:tempArray];
}
[tempArray
addObject:font];
}
detaiTV.delegate =
self;
detaiTV.dataSource =
self;
}
#pragma mark - UITableView DataSource
//表视图中存在section的个数,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return
fontArray.count;
}
//section中包含row的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [fontArray[section]
count];
}
//创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIndentifier =
@"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIndentifier];
}
cell.textLabel.text = [[fontArray
objectAtIndex:indexPath.section]
objectAtIndex:indexPath.row];
return cell;
}
//设置section头部视图的title
此方法与自定义的头部视图不能并存
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *title = [NSString
stringWithFormat:@"第%ld个section",(long)section+1];
return title;
}
//设置section尾部视图的title
此方法与自定义的尾部视图不能并存
//- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
// return @"-----------";
//}
#pragma mark - UITableView Delegate
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row ==
0 && indexPath.section ==
2 ) {
return
80;
//第三个section的第一行
}return 44;
}
//设置section头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section ==
0) {
return 80;
}return 25;
}
//设置section底部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section ==
1) {
return 100;
}return 25;
}
//设置section自定义的头部视图
//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
// UIView *headerView = [[UIView alloc]initWithFrame:CGRectZero]; //设置x,y,长,宽,都为0
// headerView.backgroundColor = [UIColor orangeColor];
// return headerView;
//}
//设置section自定义的尾部视图
默认高度为25
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView = [[UIView
alloc]initWithFrame:CGRectZero];
//设置x,y,长,宽,都为0
footerView.backgroundColor = [UIColor
cyanColor];
UILabel *tipLabel = [[UILabel
alloc]initWithFrame:CGRectMake(0,
0, 200,
25)];
tipLabel.text =
@"***********";
[footerView addSubview:tipLabel];
return footerView;
}
#define VIEW_WIDTH self.view.bounds.size.width
#define VIEW_HEIGHT self.view.bounds.size.height
#import "DetaiViewController.h"
@interface HomeViewController :
UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
}
@property (nonatomic,retain)
NSArray *listArray;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
_listArray =
@[@"UITableViewStylePlain",@"UITableViewStyleGrouped"];
_tableView = [[UITableView
alloc]initWithFrame:self.view.bounds
style:UITableViewStylePlain];
[self.view
addSubview:_tableView];
//设置数据源
_tableView.dataSource =
self;
//设置委托
_tableView.delegate =
self;
}
#pragma mark - UITableView DataSource
//表视图中存在section的个数,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//section中包含row的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return
_listArray.count;
}
//创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIndentifier =
@"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIndentifier];
}
cell.textLabel.text =
_listArray[indexPath.row];
return cell;
}
#pragma mark - UITableView delegate
//当用户选择某一行时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetaiViewController *detaiVC = [[DetaiViewController
alloc]init];
//获取点击内容
detaiVC.isPlain = indexPath.row ?
NO : YES ;
[self.navigationController
pushViewController:detaiVC animated:YES];
}
@interface DetaiViewController :
UIViewController<UITableViewDelegate,UITableViewDataSource>{
NSArray *array;
NSMutableArray *tempArray;
NSMutableArray *fontArray;
}
@property (nonatomic,assign)
BOOL isPlain;
@end
@implementation DetaiViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
array = [UIFont
familyNames];
//根据选择内容,使用表格的类型
UITableViewStyle style =
self.isPlain ?
UITableViewStylePlain :UITableViewStyleGrouped ;
NSLog(@"%ld",(long)style);
UITableView *detaiTV = [[UITableView
alloc]initWithFrame:self.view.bounds
style:style];
[self.view
addSubview:detaiTV];
fontArray = [[NSMutableArray
alloc]init];
for (int i =
0; i<array.count; i++) {
//取出字体
NSString *font =
array[i];
//将5整除的时候,创建tempArray数据,添加至fontArray中
if (i %5 ==
0) {
tempArray = [[NSMutableArray
alloc]init];
[fontArray
addObject:tempArray];
}
[tempArray
addObject:font];
}
detaiTV.delegate =
self;
detaiTV.dataSource =
self;
}
#pragma mark - UITableView DataSource
//表视图中存在section的个数,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return
fontArray.count;
}
//section中包含row的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [fontArray[section]
count];
}
//创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIndentifier =
@"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIndentifier];
}
cell.textLabel.text = [[fontArray
objectAtIndex:indexPath.section]
objectAtIndex:indexPath.row];
return cell;
}
//设置section头部视图的title
此方法与自定义的头部视图不能并存
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *title = [NSString
stringWithFormat:@"第%ld个section",(long)section+1];
return title;
}
//设置section尾部视图的title
此方法与自定义的尾部视图不能并存
//- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
// return @"-----------";
//}
#pragma mark - UITableView Delegate
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row ==
0 && indexPath.section ==
2 ) {
return
80;
//第三个section的第一行
}return 44;
}
//设置section头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section ==
0) {
return 80;
}return 25;
}
//设置section底部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section ==
1) {
return 100;
}return 25;
}
//设置section自定义的头部视图
//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
// UIView *headerView = [[UIView alloc]initWithFrame:CGRectZero]; //设置x,y,长,宽,都为0
// headerView.backgroundColor = [UIColor orangeColor];
// return headerView;
//}
//设置section自定义的尾部视图
默认高度为25
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView = [[UIView
alloc]initWithFrame:CGRectZero];
//设置x,y,长,宽,都为0
footerView.backgroundColor = [UIColor
cyanColor];
UILabel *tipLabel = [[UILabel
alloc]initWithFrame:CGRectMake(0,
0, 200,
25)];
tipLabel.text =
@"***********";
[footerView addSubview:tipLabel];
return footerView;
}
相关文章推荐
- HBase 4、Phoenix安装和Squirrel安装
- 获得UILable的高度
- iOS UISlider和 UISwitch的例子
- iOS UISegmentedControl的例子
- UIStepper 缩放:UI的使用
- UESTC 1282 被兵暴的沈宝宝 Catalan数&&逆元
- 动态规划 Longest Ordered Subsequence
- 《Effective Java》Builder
- UITableView代理方法
- UISegmentedControl(人物简介)
- 转:String StringBuffer StringBuilder区别
- UI sigment的属性和方法及案例
- 58-题目1442:A sequence of numbers
- Leetcode 225 Implement Stack using Queues STL
- uestc--758--P酱的冒险旅途
- iOS之获取UITableViewCell中UITextField的值方法总结(二)
- UGUI(七)_UI节点制作
- 【原】[UIImage imageWithContentsOfFile:]引发的图片无法显示的问题
- Android--UI
- 【转】NGUI制作按钮图标