【iOS】知乎日报第二周总结
一、无限cell的创建
1. 基本思路
无限cell的主体思路:滑到cell底端的时候,请求前一天的数据存储到数组中。然后,通过[self.tableview reloadData]更新tableview中section的cell数量,从而实现无限cell的创建,使得cell可以无限下滑加载。
具体代码如下:
- (void) loadNewData {
[[Manger1 shareManger] makeData:^(TestModel * _Nonnull ViewModel) {
for (int i = 0; i < 5; i++) {
[self.titleArray1 addObject:[ViewModel.stories[i] title]];
[self.hintArray1 addObject:[ViewModel.stories[i] hint]];
[self.imagesURL addObject:[ViewModel.stories[i] images]];
[self.imageurl addObject: self.imagesURL[self.count*5+i][0]];
}
[self.data addObject:[NSString stringWithFormat:@"Section%ld", self.count]];
self.count++;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.tableview reloadData];
});
} error:^(NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
这样就可以实现cell的无限加载,并且同时实现了之前的数据的请求。
二、top_Stories的点击事件的加载
在知乎日报的首页部分上方的无限轮播图中,需要给每一个图片添加点击事件。使得每个top_Stories都可以点击查看详情,因此需要给每个点击事件都添加tag值,就可以通过tag值来判断点击的是哪一个图片,从而进入到相应的top_Stories中。
具体代码如下:
UIImageView* imageview = [[UIImageView alloc] initWithImage:self.imageArray[4]];
imageview.frame = CGRectMake(394*i, 0, 394, 433);
imageview.tag = 105;
imageview.userInteractionEnabled =YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)];
[imageview addGestureRecognizer:tapGesture];
[self.scrollview addSubview:imageview];
self.label1.text = self.titleArray[4];
self.label2.text = self.hintArray[4];
self.label1.textColor = UIColor.whiteColor;
self.label1.font = [UIFont systemFontOfSize:25];
self.label1.numberOfLines = 0;
self.label2.font = [UIFont systemFontOfSize:15];
self.label2.textColor = UIColor.whiteColor;
self.label2.numberOfLines = 0;
self.label1.frame = CGRectMake(394*i+20, 310, 354, 70);
self.label2.frame = CGRectMake(394*i+20, 380, 200, 15);
[self.scrollview addSubview:self.label1];
[self.scrollview addSubview:self.label2];
} else if (i == 6) {
UIImageView* imageview = [[UIImageView alloc] initWithImage:self.imageArray[0]];
imageview.frame = CGRectMake(394*i, 0, 394, 433);
imageview.tag = 101;
imageview.userInteractionEnabled =YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)];
[imageview addGestureRecognizer:tapGesture];
[self.scrollview addSubview:imageview];
self.label1.text = self.titleArray[0];
self.label2.text = self.hintArray[0];
self.label1.font = [UIFont systemFontOfSize:25];
self.label1.textColor = UIColor.whiteColor;
self.label1.numberOfLines = 0;
self.label2.font = [UIFont systemFontOfSize:15];
self.label2.textColor = UIColor.whiteColor;
self.label2.numberOfLines = 0;
self.label1.frame = CGRectMake(394*i+20, 310, 354, 70);
self.label2.frame = CGRectMake(394*i+20, 380, 200, 15);
[self.scrollview addSubview:self.label1];
[self.scrollview addSubview:self.label2];
} else {
UIImageView* imageview = [[UIImageView alloc] initWithImage:self.imageArray[i-1]];
imageview.frame = CGRectMake(394*i, 0, 394, 433);
imageview.tag = 100+i;
imageview.userInteractionEnabled =YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
[tapGesture addTarget:self action:@selector(imageTap:)];
[imageview addGestureRecognizer:tapGesture];
NSLog(@"%ld", tapGesture.view.tag);
[self.scrollview addSubview:imageview];
self.label1.text = self.titleArray[i-1];
self.label2.text = self.hintArray[i-1];
self.label1.font = [UIFont systemFontOfSize:25];
self.label1.textColor = UIColor.whiteColor;
self.label1.numberOfLines = 0;
self.label2.font = [UIFont systemFontOfSize:15];
self.label2.textColor = UIColor.whiteColor;
self.label2.numberOfLines = 0;
self.label1.frame = CGRectMake(394*i+20, 310, 354, 70);
self.label2.frame = CGRectMake(394*i+20, 380, 200, 15);
[self.scrollview addSubview:self.label1];
[self.scrollview addSubview:self.label2];
}
}
[cell.contentView addSubview: _scrollview];
在给图片添加tag值的时候,出现了一个问题:具体情况就是通过下面的方式给点击手势赋tag值,但是打印出来的tag值为0。
错误代码如下:
UIImageView* imageview = [[UIImageView alloc] initWithImage:self.imageArray[i-1]];
imageview.frame = CGRectMake(394*i, 0, 394, 433);
imageview.userInteractionEnabled =YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
tapGesture.view.tag = 100+i;
[tapGesture addTarget:self action:@selector(imageTap:)];
[imageview addGestureRecognizer:tapGesture];
通过查找资料发现了问题所在,原因是因为你正在尝试为 tapGesture.view 的 tag 属性赋值,但是在这个时刻,tapGesture.view 还没有关联的视图。因此,tag 属性的赋值操作不会生效,而默认值为 0。
经过调整之后的代码如下 :
UIImageView* imageview = [[UIImageView alloc] initWithImage:self.imageArray[i-1]];
imageview.frame = CGRectMake(394*i, 0, 394, 433);
imageview.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)];
tapGesture.view.tag = 100+i; // 为手势识别器关联的视图设置 tag 值
[imageview addGestureRecognizer:tapGesture];
NSLog(@"%ld", tapGesture.view.tag);
三、WKWebView的简单使用以及注意事项
1.什么是WKWebView?
WKWebView 是 iOS 中用于加载和展示网页内容的视图组件,具有高性能、支持现代 Web 标准和 JavaScript 交互能力,以及增强的安全性。它为开发人员提供了强大的功能和灵活性,使得在应用程序中集成网页内容变得更加便捷和高效。
2.怎样使用WKWebView?
这里只有说明一些简单的使用方法。
具体代码如下:
//通过`[[WKWebView alloc] initWithFrame:]`创建一个WKWebView对象,并将其大小设置为当前屏幕的大小。
WKWebView* webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//将当前视图设置为刚创建的WKWebView对象,这样WebView将成为应用界面的一部分。
self.view = webView;
// 创建URL对象:
NSURL *url = [NSURL URLWithString:@"https://daily.zhihu.com/story/9766643"];
// 创建URL请求:
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 加载URL请求:
[webView loadRequest:request];
3.使用WKWebView的注意事项
- 需要导入WebKit框架:确保在代码中导入了WebKit框架,以便使用WKWebView。可以通过在代码文件的开头添加#import <WebKit/WebKit.h>来导入框架。
- 设置WKWebView的frame和位置:在创建WKWebView对象时,需要设置其frame属性,以确定WebView在界面上的位置和大小。确保将其添加到正确的视图层次结构中,并在界面上正确布局。
- 处理导航动作:WKWebView可以加载网页并处理用户的导航动作,例如点击链接、后退、前进等。可以通过实现WKNavigationDelegate协议的相关方法来拦截导航动作、处理页面加载状态、处理错误等。
- 处理网页加载错误:在加载网页时,可能会遇到加载失败、网络错误等情况。需要适当处理这些错误,例如显示错误提示、重新加载网页等。
- 安全性考虑:当加载来自不可信的源或用户提供的内容时,需要注意安全性问题,以防止恶意代码注入或跨站脚本攻击。可以使用WKWebView的安全设置和内容过滤机制来增强安全性。
四、隐藏导航栏
在Objective-C中隐藏导航栏,可以使用以下代码:
[self.navigationController setNavigationBarHidden:YES animated:YES];
上述代码将导航栏隐藏,并且会以动画的方式进行过渡。如果你希望没有过渡动画,可以将animated参数设置为NO。
请注意,在隐藏或显示导航栏时,应确保在正确的位置调用相应的方法。通常,你可以在视图控制器的 viewWillAppear: 或 viewDidAppear: 方法中调用适当的导航栏隐藏或显示方法,以确保正确的过渡效果。