UITabView
UITabView可是实现列表功能,此文转自https://www.cnblogs.com/longiang7510/p/5367080.html,讲述很详细,都有注视,但是注释解释不太确切,可以自行测试体会
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化数据
NSArray *array1_=@[@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"];
NSArray *array2_=@[@"李小龙",@"李小路"];
NSArray *array3_=@[@"王刚"];
self.myDic=@{@"老张家":array1_, @"老李家":array2_, @"老王家":array3_};
UITableView *myTableView_=[[UITableView alloc] initWithFrame:CGRectMake(, ,, ) style:UITableViewStylePlain];
myTableView_.delegate=self;
myTableView_.dataSource=self;
//改变换行线颜色lyttzx.com
myTableView_.separatorColor = [UIColor blueColor];
//设定Header的高度,
myTableView_.sectionHeaderHeight=;
//设定footer的高度,
myTableView_.sectionFooterHeight=;
//设定行高
myTableView_.rowHeight=;
//设定cell分行线的样式,默认为UITableViewCellSeparatorStyleSingleLine
[myTableView_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
//设定cell分行线颜色
[myTableView_ setSeparatorColor:[UIColor redColor]];
//编辑tableView
myTableView_.editing=NO;
[self.view addSubview:myTableView_];
//跳到指的row or section
[myTableView_ scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2inSection:]
atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.myDic allKeys] count];
}
//每个section底部标题高度(实现这个代理方法后前面 sectionHeaderHeight 设定的高度无效)
//上述为原作者注视,个人认为section的标题高度,当设置为0时,标题高度为0
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return ;
}
//每个section头部标题高度(实现这个代理方法后前面 sectionFooterHeight 设定的高度无效)
//上述为原作者注释,个人认为为每一个section结束后与下一个section之间的间距
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return ;
}
//每个section头部的标题-Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [[self.myDic allKeys] objectAtIndex:section];
}
//每个section底部的标题-Footer
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return nil;
}
//用以定制自定义的section头部视图-Header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return nil;
}
//用以定制自定义的section底部视图-Footer
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIImageView *imageView_=[[UIImageView alloc]initWithFrame:CGRectMake(, ,, )];
imageView_.image=[UIImage imageNamed:@"1000.png"];
return [imageView_ autorelease];
}
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.myDic objectForKey:[[self.myDic allKeys]objectAtIndex:section]]count];
}
//改变行的高度(实现主个代理方法后 rowHeight 设定的高度无效)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ;
}
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SimpleTableIdentifier] autorelease];
//设定附加视图
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
// UITableViewCellAccessoryNone, // 没有标示
// UITableViewCellAccessoryDisclosureIndicator, // 下一层标示
// UITableViewCellAccessoryDetailDisclosureButton, // 详情button
// UITableViewCellAccessoryCheckmark // 勾选标记
//设定选中cell时的cell的背影颜色
cell.selectionStyle=UITableViewCellSelectionStyleBlue; //选中时蓝色效果
// cell.selectionStyle=UITableViewCellSelectionStyleNone; //选中时没有颜色效果
// cell.selectionStyle=UITableViewCellSelectionStyleGray; //选中时灰色效果
//
// //自定义选中cell时的背景颜色
// UIView *selectedView = [[UIView alloc] initWithFrame:cell.contentView.frame];
// selectedView.backgroundColor = [UIColor orangeColor];
// cell.selectedBackgroundView = selectedView;
// [selectedView release];
// cell.selectionStyle=UITableViewCellAccessoryNone; //行不能被选中
}
//这是设置没选中之前的背景颜色
cell.contentView.backgroundColor = [UIColor clearColor];
cell.imageView.image=[UIImage imageNamed:@"1001.jpg"];//未选cell时的图片
cell.imageView.highlightedImage=[UIImage imageNamed:@"1002.jpg"];//选中cell后的图片
cell.textLabel.text=[[self.myDic objectForKey:[[self.myDicallKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];
return cell;
}
//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
return row;
}
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
//得到当前选中的cell
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell=%@",cell);
}
//已经选中行(常用)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//作用:跳转下一个页面,并且把当前选中的单元格数据,传递过去
//寻找选中的单元格
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// //通过cell获取indexPath
// NSIndexPath *path = [tableView indexPathForCell:cell];
QYDetailInfoViewController *detailInfoVC = [[QYDetailInfoViewController alloc] init];
detailInfoVC.titleString = cell.textLabel.text;
[self.navigationController pushViewController:detailInfoVC animated:YES];
}
//行将显示的时候调用,预加载行
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"将要显示的行是\n cell=%@ \n indexpath=%@",cell,indexPath);
}
//选中之前执行,判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (row == )
return nil;
return indexPath;
}
//编辑状态,点击删除时调用
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
//cell右边按钮格式为UITableViewCellAccessoryDetailDisclosureButton时,点击按扭时调用的方法
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"当前点击的详情button \n indexpath=%@",indexPath);
}
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [self.myDic allKeys];
}
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
return YES;
}
//设定横向滑动时是否出现删除按扭,(阻止第一行出现)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==) {
return UITableViewCellEditingStyleNone;
}
else{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleDelete;
}
//自定义划动时delete按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除这行";
}
//开始移动row时执行
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{
NSLog(@"sourceIndexPath=%@",sourceIndexPath);
NSLog(@"sourceIndexPath=%@",destinationIndexPath);
}
//滑动可以编辑时执行
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"willBeginEditingRowAtIndexPath");
}
//将取消选中时执行, 也就是上次先中的行
-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"上次选中的行是 \n indexpath=%@",indexPath);
return indexPath;
}
//让行可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath
{
return NO;
}
//移动row时执行
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath
{
NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");
//用于限制只在当前section下面才可以移动
if(sourceIndexPath.section != proposedDestinationIndexPath.section){
return sourceIndexPath;
}
return proposedDestinationIndexPath;
}
下述为一个自己app的应用管理,把一些常用的app作了简单集成
//
// ViewController.m
// Win
//
// Created by apple on 2018/10/12.
// Copyright © 2018年 apple. All rights reserved.
// #import "ViewController.h" #define MenuTitle @"Created by LL" @interface ViewController ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) NSArray *sections;
@property (nonatomic, strong) NSArray *appUrls;
//@property (nonatomic, strong) NSArray *classNames;
@property (nonatomic, strong) UITableView *tableView; -(void) launchApp:(NSString *)appUrl; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor=[UIColor brownColor];
//self.title=MenuTitle;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , [[UIScreen mainScreen] bounds].size.width, )];
label.text = MenuTitle;
label.textAlignment=;
//label.backgroundColor = [UIColor whiteColor];
self.navigationItem.titleView = label; [self initTitles];
[self initAppUrls]; [self initTableView]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Initialization - (void)initTitles
{
NSString *sec1Title = @"生活";
NSArray *sec1CellTitles = @[@"饿了么",@"美团",@"京东",@"点评",@"微信",@"支付宝"];
NSArray *section1 = @[sec1Title, sec1CellTitles]; NSString *sec3Title = @"应用";
NSArray *sec3CellTitles = @[@"打电话",@"苹果地图",@"谷歌浏览器",@"设置相关",@"Wps阅读"];
NSArray *section3 = @[sec3Title, sec3CellTitles]; NSString *sec5Title = @"娱乐";
NSArray *sec5CellTitles = @[@"优酷",@"知乎",@"网易音乐"];
NSArray *section5 = @[sec5Title, sec5CellTitles]; self.sections = [NSArray arrayWithObjects:section1, section3, section5, nil];
} - (void)initAppUrls
{
NSArray *sec1CellTitles = @[@"eleme://",@"iMeituan://",@"openApp.jdMobile://",@"dianping://",@"weixin://",@"alipay://"]; NSArray *sec3CellTitles = @[@"telprompt://%@",@"Map",@"googlechrome://",@"Settings",@"KingsoftOfficeApp://"]; NSArray *sec5CellTitles = @[@"youku://",@"zhihu://",@"yinyue://"]; self.appUrls = [NSArray arrayWithObjects:sec1CellTitles, sec3CellTitles, sec5CellTitles, nil];
} //- (void)initClassNames
//{
// NSArray *sec1ClassNames = @[@"DriveRoutePlanViewController",
// @"WalkRoutePlanViewController"];
//
// NSArray *sec2ClassNames = @[@"GPSNaviViewController",
// @"EmulatorNaviViewController",
// @"DetectedModeViewController",
// @"GPSEmulatorViewController"];
//
// NSArray *sec3ClassNames = @[@"HUDNaviViewController"];
//
// NSArray *sec4ClassNames = @[@"CustomCarCompassViewController",
// @"CustomRouteTrafficPolylineViewController",
// @"CustomRoutePolylineViewController",
// @"CustomTurnInfoViewController",
// @"CustomCrossLaneInfoViewController",
// @"CustomFunctionalButtonViewController",
// @"CustomTrackingModeViewController"];
//
// NSArray *sec5ClassNames = @[@"MultiRoutePlanViewController",
// @"QuickStartViewController"];
//
// self.classNames = [NSArray arrayWithObjects:sec1ClassNames, sec2ClassNames, sec3ClassNames, sec4ClassNames, sec5ClassNames, nil];
//} - (void)initTableView
{
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tableView.sectionHeaderHeight=;
self.tableView.backgroundColor=[UIColor brownColor];
self.tableView.delegate = self;
self.tableView.dataSource = self; [self.view addSubview:self.tableView];
} -(void) launchApp:(NSString *)appUrl
{
if([appUrl isEqualToString:@"Settings"]==YES)
{
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url])
{
NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
return;
} if([appUrl isEqualToString:@"Map"]==YES)
{
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
{
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] init];
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}]; }
return;
} if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appUrl]])
{
//NSString *urlString = [NSString stringWithFormat:appUrl];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUrl]];
}} #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _sections.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_sections[section][] count];
} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return _sections[section][];
} -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.textColor = [UIColor redColor];
header.contentView.backgroundColor = [UIColor yellowColor];
} -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return ;
} -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *mainCellIdentifier = @"mainCellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mainCellIdentifier]; if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:mainCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} cell.textLabel.text = _sections[indexPath.section][][indexPath.row];
cell.backgroundColor= [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:0.8];
//cell.detailTextLabel.text = self.classNames[indexPath.section][indexPath.row]; return cell;
} #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES]; //NSString *string=[NSString stringWithFormat:@"section %ld row %ld",indexPath.section,indexPath.row]; //NSLog(@"section %ld,row %ld",indexPath.section,indexPath.row);
NSLog(self.appUrls[indexPath.section][indexPath.row]); [self launchApp:self.appUrls[indexPath.section][indexPath.row]]; //UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//NSLog(cell.textLabel.text);
//NSString *className = self.classNames[indexPath.section][indexPath.row]; //UIViewController *subViewController = [[NSClassFromString(className) alloc] init]; //subViewController.title = _sections[indexPath.section][1][indexPath.row]; //[self.navigationController pushViewController:subViewController animated:YES];
} @end
UITabView的更多相关文章
- 【iOS】UITabView/UICollectionView 全选问题
UITabView/UICollectionView 全选问题 SkySeraph July. 30th 2016 Email:skyseraph00@163.com 更多精彩请直接访问SkySera ...
- iOS关于UITabView和UIAlertController,UIAlertAction以及UINavigation,值修改的传递页面推送
关于UITabView和UIAlertController,UIAlertAction以及UINavigation,值修改的传递 集合嵌套集合的操作 声明 两个必须的的代理 实现部分代码 - (voi ...
- 数组实现UITabview的cell设置
- iOS UITabView简写瀑布流
代码demo 一.tabViewCell,通过image的比例高算出cell 的高度 #import "TableViewCell.h" @implementation Table ...
- UITabView使用详解
在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况 - (void)viewDidLoad { [super viewDidL ...
- iOS系列 基础篇 05 视图鼻祖 - UIView
iOS系列 基础篇 05 视图鼻祖 - UIView 目录: UIView“家族” 应用界面的构建层次 视图分类 最后 在Cocoa和Cocoa Touch框架中,“根”类时NSObject类.同样, ...
- 浅谈JavaScript、ES5、ES6
// http://es6.ruanyifeng.com/#docs/intro (ES6 文档) 什么是JavaScript JavaScript一种动态类型.弱类型.基于原型的客户端脚本语言,用来 ...
- iOSCoreData介绍
1.CoreData简介 Coredata用作数据持久化,使和大数据量的存储和查询 虽然是用户做数据的保存,但是并不是数据库,CoreData可以使用数据库.XML来存储数据 SQLite通过SQL语 ...
- iOS上机题(附个人见解)
##机试题目如下 用命令行创建一个以CocoaPods管理的项目[Test-你的姓名拼音],新建3个ViewController,完成以下题目 将下面的问题在一个UITabView里面列出所有问题,单 ...
随机推荐
- 性能测试之 Gatling
在应用程序上线之前,有多少人做过性能测试? 估计大部分开发者更多地关注功能测试,并且会提供一些单元测试和集成测试的用例.然而,有时候性能漏洞导致的影响比未发现的业务漏洞更严重,因为性能漏洞影响的是整个 ...
- win7远程连接全屏和窗口模式切换
最近开发需要win7远程连接,我知道在连接的时候可以设置全屏模式 但是进去之后想要切换就只能通过快捷键了上网查了一下是ctrl+alt+break.网上说的没有错.我查官方文档也是这样.但是我按的时候 ...
- 由std::once_call 引发的单例模式的再次总结,基于C++11
一个偶然的机会,知道了std::once_call这个东西. 了解了下,std::once_call支持多线程情况下的某函数只执行一次.咦,这个不是恰好符合单例模式的多线程安全的困境吗? 单例模式,经 ...
- 移动端border-radius的几个BUG
个人博客: http://mcchen.club 一.Android 2.3 自带浏览器不支持 % 通常我们实现一个正圆只需要border-radius: 50%即可,大致代码如下 .foo { wi ...
- 在Mac上搭建带ssl协议和域名指向的Apache服务器
顾名思义,就是要在苹果电脑上搭建 Apache 服务器,并且支持 https 协议,能用指定域名访问(有些开发调试需要注册域名,比如调试微信JS-SDK),当然最好能在手机端进行调试.首先,Mac 系 ...
- 最简单的JS实现json转csv
工作久了,总会遇到各种各样的数据处理工作,比如同步数据,初始化一些数据,目前比较流行的交互数据格式就是JSON,可是服务器中得到的JSON数据如果提供给业务人员看的话可能会非常不方便,这时候,转成CS ...
- Java字段初始化规律
首先先附上一段代码:public class InitializeBlockDemo { public static void main(String[] args) { InitializeBloc ...
- JavaScript事件属性event.target和currentTarget 属性的区别。
event.target 获取的是触发事件的标签元素 event.currentTarget 获取到的是发起事件的标签元素 一.事件属性:event.target target事件委托的定义:本来该自 ...
- Java之微信公众号开发
这次以文本回复作为案例来讲解Java相关得微信公众号开发. 首先必须要有一个个人微信公众号 个人微信公众号相关的接口权限有限,不过用于个人学习体验一下足够了,如图: 然后进入微信公众后台,点击基本配置 ...
- 微信小程序学习总结
微信小程序开发环境安装以及相关设置配置 微信小程序前端页面书写 微信小程序前端样式WXSS书写 微信小程序中事件 微信小程序自定义组件 微信小程序发起请求 微信小程序登入流程 微信小程序路由跳转 微信 ...