iOS之UITableViewController使用详解(一)
2016-04-30 23:05
295 查看
UITableView的多组显示
#import "ViewController.h"
@interface
ViewController ()<UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//tintColor 很多控件都有
//
设置tableView的索引标题颜色
//self.tableView.sectionIndexColor = [UIColor redColor];
// 设置tableView的索引标题背景颜色
//self.tableView.sectionIndexBackgroundColor= [UIColor redColor];
// 设置tableView的索引标题选中时的颜色
//self.tableView.sectionIndexTrackingBackgroundColor= [UIColor redColor];
//
设置主题颜色 tintColor
self.tableView.tintColor =HMColor(21,188,173);
}
// 有多少组在tableView中
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
// 每一组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
/**
因为每一组显示的行数不一样,所以要对每一组进行判断
*/
NSInteger rows =
0;
if (section ==
0) {
rows = 30;
} else if (section ==1) {
rows = 20;
} else {
rows = 10;
}
return rows;
}
// 每一行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
实例化cell
UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];
/**
indexPath.row
行
indexPath.section
组
定义唯一的一行
*/
//
把组取出来
NSInteger sectionIndex = indexPath.section;
if (sectionIndex ==
0) { // 第0组
//
判断第0组的第几行
NSInteger rowIndex = indexPath.row;
if (rowIndex ==
0) {
cell.textLabel.text =@"火影:鸣人";
} else if (rowIndex ==1) {
cell.textLabel.text =@"上忍:卡卡西";
} else {
cell.textLabel.text =@"中忍:李";
}
} else if (sectionIndex ==1) {
// 两行
if (indexPath.row ==0) {
cell.textLabel.text =@"风影:我爱罗";
} else {
cell.textLabel.text =@"堪九郎";
}
} else {
cell.textLabel.text =@"小魔仙";
}
cell.separatorInset=UIEdgeInsetsZero;//cell的分割线不锁进
return cell;
}
/**
titleForHeaderInSection:
组的头部显示的文本
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// 判断是第几组,然后显示对应的文本
NSString *title =
@"";
switch (section) {
case 0:
{
title = @"火之国";
}
break;
case 1:
{
title = @"风之国";
}
break;
case 2:
{
title = @"巴拉巴拉";
}
break;
default:
break;
}
return title;
}
/**
titleForFooterInSection:
组的头部显示的文本
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
// 判断是第几组,然后显示对应的文本
NSString *title =
@"";
switch (section) {
case 0:
{
title = @"五影最强";
}
break;
case 1:
{
title = @"黑眼圈";
}
break;
case 2:
{
title = @"巴拉巴拉小魔仙";
}
break;
default:
break;
}
return title;
}
- (BOOL)prefersStatusBarHidden {
return
YES;
}
@end
// 设置控制器成为tableView的代理
_tableView.delegate =self;
// 设置分割线颜色的
_tableView.separatorColor = [UIColorredColor];
//
侵蚀 , 分割线样式
_tableView.separatorStyle =UITableViewCellSeparatorStyleSingleLine;
// top, left, bottom, right ,
上, 下
是没有效果的
_tableView.separatorInset =UIEdgeInsetsMake(0,0,0,0);
//
允许多选
_tableView.allowsMultipleSelection =YES;
/**
如果让tableiew自动的计算行高,那么就必须给他一个预估的行高
如果设置了预估行高,那么tableview在去加载数据的时候,不会频繁的调用
heightForRowAtIndexPath:
先调用一次 cellForRowAtIndexPath:
再调用一次 heightForRowAtIndexPath:
*/
self.tableView.estimatedRowHeight =10;
//
让UITableView 自动的去计算cell的高度
self.tableView.rowHeight =UITableViewAutomaticDimension
// 设置行高 ,(静态设置)如果每个cell的高度都一样,推荐这种设置
// _tableView.rowHeight = 100;
//设置tableView的头
UIView *headerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100,100)];
[headerView setBackgroundColor:[UIColororangeColor]];
_tableView.tableHeaderView = headerView;
//设置tableView的尾
UIView *footerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100,100)];
[footerView setBackgroundColor:[UIColoryellowColor]];
_tableView.tableFooterView = footerView;
//设置cell的样式(系统自带的)
UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];
/**
UITableViewCellStyleDefault :
不显示detailTextLabel
UITableViewCellStyleValue1
: detailLabel
显示在 textLabel
右侧
UITableViewCellStyleValue2
: imageView不再显示, textLabel居左变蓝色
UITableViewCellStyleSubtitle
:都显示, detailLabel
在 textLabel下侧
*/
// 设置cell上控件的内容
HeroModel *model =
self.dataArray[indexPath.row];
//
设置imageView
cell.imageView.image = [UIImageimageNamed:model.icon];
//
设置文本
cell.textLabel.text = model.name;
//
设置detailTextLabel
cell.detailTextLabel.text = model.intro;
//
设置右侧箭头
// accessory :
配件
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
//
设置选择样式
/**
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
用灰色来代替了
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
*/
/**
设置选中的背景view
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
tempView.backgroundColor = [UIColor yellowColor];
cell.selectedBackgroundView = tempView;
*/
// cell.backgroundColor = [UIColor yellowColor];
/**
accessoryView 自定义控件
自定义 accessoryView的时候, frame中的坐标(x,y)修改后无效
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
tempView.backgroundColor = [UIColor redColor];
cell.accessoryView = tempView;
*/
组的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"英雄";
}
// 可以对 section的header和 footer设置view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIViewalloc]init];
[headerView setBackgroundColor:[UIColorredColor]];
return headerView;
}
组标题快速索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return_indexArray;
}
// 滚动到最后一行
[_tableViewscrollToRowAtIndexPath:indexPathatScrollPosition:UITableViewScrollPositionBottomanimated:YES];
#pragma mark - 返回cell行高的代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//
要返回所有cell的行高
//
取出contentFrameModel
ContentFrameModel *frameModel =
self.dataArray[indexPath.row];
return frameModel.cellHeight;
}
=======系统默认tableView是显示分隔线的,但不幸的是,如果我们cell过少的话,是会出现多余的分隔线的,其实要处理这个问题,很简单,
我们只需要在tableView加载在视图后,加上下面这一句代码:
swift:
self.tableView?.tableFooterView = UIView()
OC:
self.tableView.tableFooterView = [[UIView alloc] init];
#import "ViewController.h"
@interface
ViewController ()<UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//tintColor 很多控件都有
//
设置tableView的索引标题颜色
//self.tableView.sectionIndexColor = [UIColor redColor];
// 设置tableView的索引标题背景颜色
//self.tableView.sectionIndexBackgroundColor= [UIColor redColor];
// 设置tableView的索引标题选中时的颜色
//self.tableView.sectionIndexTrackingBackgroundColor= [UIColor redColor];
//
设置主题颜色 tintColor
self.tableView.tintColor =HMColor(21,188,173);
}
// 有多少组在tableView中
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
// 每一组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
/**
因为每一组显示的行数不一样,所以要对每一组进行判断
*/
NSInteger rows =
0;
if (section ==
0) {
rows = 30;
} else if (section ==1) {
rows = 20;
} else {
rows = 10;
}
return rows;
}
// 每一行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
实例化cell
UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];
/**
indexPath.row
行
indexPath.section
组
定义唯一的一行
*/
//
把组取出来
NSInteger sectionIndex = indexPath.section;
if (sectionIndex ==
0) { // 第0组
//
判断第0组的第几行
NSInteger rowIndex = indexPath.row;
if (rowIndex ==
0) {
cell.textLabel.text =@"火影:鸣人";
} else if (rowIndex ==1) {
cell.textLabel.text =@"上忍:卡卡西";
} else {
cell.textLabel.text =@"中忍:李";
}
} else if (sectionIndex ==1) {
// 两行
if (indexPath.row ==0) {
cell.textLabel.text =@"风影:我爱罗";
} else {
cell.textLabel.text =@"堪九郎";
}
} else {
cell.textLabel.text =@"小魔仙";
}
cell.separatorInset=UIEdgeInsetsZero;//cell的分割线不锁进
return cell;
}
/**
titleForHeaderInSection:
组的头部显示的文本
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// 判断是第几组,然后显示对应的文本
NSString *title =
@"";
switch (section) {
case 0:
{
title = @"火之国";
}
break;
case 1:
{
title = @"风之国";
}
break;
case 2:
{
title = @"巴拉巴拉";
}
break;
default:
break;
}
return title;
}
/**
titleForFooterInSection:
组的头部显示的文本
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
// 判断是第几组,然后显示对应的文本
NSString *title =
@"";
switch (section) {
case 0:
{
title = @"五影最强";
}
break;
case 1:
{
title = @"黑眼圈";
}
break;
case 2:
{
title = @"巴拉巴拉小魔仙";
}
break;
default:
break;
}
return title;
}
- (BOOL)prefersStatusBarHidden {
return
YES;
}
@end
// 设置控制器成为tableView的代理
_tableView.delegate =self;
// 设置分割线颜色的
_tableView.separatorColor = [UIColorredColor];
//
侵蚀 , 分割线样式
_tableView.separatorStyle =UITableViewCellSeparatorStyleSingleLine;
// top, left, bottom, right ,
上, 下
是没有效果的
_tableView.separatorInset =UIEdgeInsetsMake(0,0,0,0);
//
允许多选
_tableView.allowsMultipleSelection =YES;
/**
如果让tableiew自动的计算行高,那么就必须给他一个预估的行高
如果设置了预估行高,那么tableview在去加载数据的时候,不会频繁的调用
heightForRowAtIndexPath:
先调用一次 cellForRowAtIndexPath:
再调用一次 heightForRowAtIndexPath:
*/
self.tableView.estimatedRowHeight =10;
//
让UITableView 自动的去计算cell的高度
self.tableView.rowHeight =UITableViewAutomaticDimension
// 设置行高 ,(静态设置)如果每个cell的高度都一样,推荐这种设置
// _tableView.rowHeight = 100;
//设置tableView的头
UIView *headerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100,100)];
[headerView setBackgroundColor:[UIColororangeColor]];
_tableView.tableHeaderView = headerView;
//设置tableView的尾
UIView *footerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100,100)];
[footerView setBackgroundColor:[UIColoryellowColor]];
_tableView.tableFooterView = footerView;
//设置cell的样式(系统自带的)
UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];
/**
UITableViewCellStyleDefault :
不显示detailTextLabel
UITableViewCellStyleValue1
: detailLabel
显示在 textLabel
右侧
UITableViewCellStyleValue2
: imageView不再显示, textLabel居左变蓝色
UITableViewCellStyleSubtitle
:都显示, detailLabel
在 textLabel下侧
*/
// 设置cell上控件的内容
HeroModel *model =
self.dataArray[indexPath.row];
//
设置imageView
cell.imageView.image = [UIImageimageNamed:model.icon];
//
设置文本
cell.textLabel.text = model.name;
//
设置detailTextLabel
cell.detailTextLabel.text = model.intro;
//
设置右侧箭头
// accessory :
配件
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
//
设置选择样式
/**
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
用灰色来代替了
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
*/
/**
设置选中的背景view
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
tempView.backgroundColor = [UIColor yellowColor];
cell.selectedBackgroundView = tempView;
*/
// cell.backgroundColor = [UIColor yellowColor];
/**
accessoryView 自定义控件
自定义 accessoryView的时候, frame中的坐标(x,y)修改后无效
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
tempView.backgroundColor = [UIColor redColor];
cell.accessoryView = tempView;
*/
组的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"英雄";
}
// 可以对 section的header和 footer设置view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIViewalloc]init];
[headerView setBackgroundColor:[UIColorredColor]];
return headerView;
}
组标题快速索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return_indexArray;
}
// 滚动到最后一行
[_tableViewscrollToRowAtIndexPath:indexPathatScrollPosition:UITableViewScrollPositionBottomanimated:YES];
#pragma mark - 返回cell行高的代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//
要返回所有cell的行高
//
取出contentFrameModel
ContentFrameModel *frameModel =
self.dataArray[indexPath.row];
return frameModel.cellHeight;
}
=======系统默认tableView是显示分隔线的,但不幸的是,如果我们cell过少的话,是会出现多余的分隔线的,其实要处理这个问题,很简单,
我们只需要在tableView加载在视图后,加上下面这一句代码:
swift:
self.tableView?.tableFooterView = UIView()
OC:
self.tableView.tableFooterView = [[UIView alloc] init];
相关文章推荐
- UICollectionViewController的使用详解,相册滚动偏移放大
- EasyUI DataGird底部统计行
- UITableView的registerClass forCellReuseIdentifier释义
- ZOJ-3929 Deque and Balls (DP+找规律)
- 关于使用UIPanGestureRecognizer手势touchesBegan不调用的问题
- android-----在子线程中更新UI操作的方法
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics
- Android-通知Notification、Notification.Builder和NotificationCompat.Builder兼容
- SDUT 2416:Fruit Ninja II
- STL 源码剖析 queue
- 用arduino和热敏电阻做电子体温计
- Longest Increasing Subsequence Review
- UIScrollView 的代理方法简单注解
- android四种更新UI进程
- 54.java编程思想——创建窗口和程序片 逻辑与UI分离
- 提高UI流畅度
- Codeforces #349 div1 A. Reberland Linguistics 模拟 递推
- LIGHTOJ1048_Conquering Keokradong
- requires the IDE to be running with java 1.8 or later 解决方法
- 225. Implement Stack using Queues