文章目录
预加载界面的跳转
一 如何实现定时跳转界面剂以及NSTimer的简单使用
-
- 目的是在这个界面停留一定的时间然后跳转到如下界面
- - 思路:首先要展示的肯定是加载的界面,我们在ViewContrller文件里先把初始界面放上去
UIImageView* imageSelf = [[UIImageView alloc]init];
imageSelf.image = [UIImage imageNamed:@"UIIMG.jpeg"];
imageSelf.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:imageSelf];
- 之后在ViewContrller设定时器⏲️
-
- 3.0是指在该界面停留时间
-
- -
selector:@selector(timeOut)
是时间结束之后的事件函数
- -
-
- -
repeats:NO
指 不重复 这里只是简单的使用 不需要重复该行为
- -
// 定时器
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timeOut) userInfo:nil repeats:NO];
// 手动添加到Runloop
[[NSRunLoop currentRunLoop]addTimer:myTimer forMode:NSDefaultRunLoopMode];
- 定时器结束事件函数
- (void)timeOut
-
- 在函数里设置四个导航栏和分栏控制器,也就是我们仿写的四个界面,接下来需要把根视图改成这四个界面。需要注意的是,我们刚开始的默认RootViewCOntroller是我们的预加载界面,这时候如何切换到主界面?
预加载到分栏控制器,用定时器改变根视图
- 在iOS.15版本之下有如下方法获取winodw
UIWindow * window = [UIApplication sharedApplication].windows[0]; window.rootViewController = tabController;
- -关于设置App一进来的那个图片,有多种方式,这里我推荐用一种,定时器结束后改变根视图。获取根视图的代码
UIWindow * window = [UIApplication sharedApplication].windows[0];
window.rootViewController = tabController;
二. 关于夜间模式控件刷新就回到本身的问题-Cell复用的简单介绍
- 如上图所示,刚开始我没有用自定义的Cell来写这个UISwitch控件,导致我如果先打开这个控件,在上下滑动的时候就会变回灰色,这里了解了相关知识,关于UITableVIewCEll的复用机制
- UITableView(表视图) 是在开发中经常使用的控件,而且有时处理的内容量也是非常巨大的,这就需要考虑表视图的性能优化,而最基本的性能优化则是单元格的复用。
关于Cell的复用
复用Cell的来源 -自己手动创建新的单元格
- 这是刚开始写的Cell
NSString* s = @"strId";
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCell* cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:s];
if (cell == nil) {
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:s]; }
// 添加小控件
UISwitch* switchDeep = [[UISwitch alloc] init];
这样自己创建的Cell单元格就会引起复用
复用机制
- 在UITableView(表视图)显示的时候,会创建 (视图中可看的单元格个数+1)个单元格,一旦单元格因为滑动的而消失在我们的视野中的时候,消失的单元格就会进入缓存池(或叫复用池),当有新的单元格需要显示的时候,会先从缓存池中取可用的单元格,获取成功则使用获取到的单元格,获取失败则重新创建心的单元格,这就是整个的复用机制。
- 简单来说,当创建多个cell并且屏幕显示不了所有的cell时,系统会将多余没有显示到的cell放入复用池(reuse pool)中,当需要新建cell并且标识符与复用池中的标识符存在相同的情况,就会调用复用池中的cell,并且会使用该cell的所有UI控件
-
- 所以在上下滑动的时候我们调用的是复用池的单元格相当于重新建了一个 所以控件就会返回到刚开始的样子
解决-自定义Cell(方法之一)
- 这里我真对这个问题有如下方法,Cell的复用有很多机制解决。这里只关于Cell的自定义
- 这里我们自定义一个Cell来解决刚才的问题
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface EightTableViewCell : UITableViewCell
@property (nonatomic, strong) UILabel *FirstLabel;
@property (nonatomic, strong) UILabel *SecondLabel;
@property (nonatomic, strong) UIScrollView *scrollView;
@end
-
- 三个基础控件足以,接着实现.m文件里的两个函数
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier: reuseIdentifier];
//添加右侧箭头
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
self.FirstLabel = [[UILabel alloc] init];
[self.contentView addSubview: _FirstLabel];
self.FirstimageView = [[UIImageView alloc] init];
[self.contentView addSubview: _FirstimageView];
self.Switch = [[UISwitch alloc] init];
[self.contentView addSubview: _Switch];
self.backgroundColor = [UIColor whiteColor];
return self;
}
- (void)layoutSubviews {
_FirstimageView.frame = CGRectMake(15, 10, 35, 35);
_FirstLabel.frame = CGRectMake(70, 0, 200, 60);
_Switch.frame = CGRectMake(320, 20, 100, 60);
}
- -之后需要的界面注册并添加到TableView里即可
-
- 注册
[self.tableView registerClass:[SevenTableViewCell class] forCellReuseIdentifier:@"seven"];
-
- 添加
else if (indexPath.row == 7) {
SevenTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"seven" forIndexPath:indexPath];
cell.FirstLabel.text = @"夜间模式";
cell.FirstimageView.image = [UIImage imageNamed: @"dark.png"];
return cell;
}
换头像- 简单的代理传值
- 之前写过一片简单的关于传值问题iOS界面传值-代理传值,刚开始复习的时候有点蒙圈,上网查了一些资料才完成了换头像
- 思路:在我的界面点击头像BUtton 跳转到一个头像存储界面类似于照片墙,里面可以存放Button,点击这些ImageButton即可实现换头像