这周主要完成了TableView的滑动刷新,cell的点击事件和cell的滑动刷新
1.对于TableView的滑动刷新,当TableView被滑动到最底部时,就根据最底部一天的文章的日期网络获取获取前一天的内容;
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if (self.homeTableView.contentOffset.y + self.homeTableView.contentSize.height > self.homeTableView.contentSize.height && self.k == 0) {
self.k = 1;
[self.activityIndicator startAnimating];
self.model1 = [self.dictinaryArray lastObject];
NSString* date = self.model1.date;
NSDictionary* dictionary = [NSDictionary dictionaryWithObject:date forKey:@"date"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"dateSender" object:nil userInfo:dictionary];
[[Manager shareManager] makeData2:^(ContentsModel * _Nonnull ViewModel1) {
[self.dictinaryArray addObject:ViewModel1];
[self.homeTableView reloadData];
[self.activityIndicator stopAnimating];
} error:^(NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
self.k = 0;
}
}
2.对于点击滚动视图时无法触发cell的点击事件,在自定义UITableViewCell 和UICollectionViewCell的时候,需要有个scrollerView滑动的时候,会发现 didSelectItemAtIndexPath: indexPath 这个点击方法被覆盖了,不能被执行。
解决办法:
在自定义Cell中添加下面代码:
self.scrollView.userInteractionEnabled = NO;
[self.contentView addGestureRecognizer: self.scrollView.panGestureRecognizer];
这样子就可以完美解决Cell的点击事件被覆盖无法执行的问题了
3.在点击cell时想跳转到其他页面,在cell的点击事件里面推出新的页面发现没有效果,由于cell的点击事件被写在了view层无法去1实现跳转,需要写一个通知传值,让controller 接受通知并实现跳转。
4.对于cell的具体内容会用到WKWebView,需要导入头文件
#import <WebKit/WebKit.h>
具体的使用代码
self.wkWebView = [[WKWebView alloc] init];
[self.topStoriesScrollView addSubview:self.wkWebView];
[self.wkWebView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.topStoriesScrollView).offset(50);
make.left.equalTo(self.topStoriesScrollView).offset(i * widt);
make.width.offset(widt);
make.height.offset(heigh - 50);
}];
NSURL* url = [NSURL URLWithString:self.urlarray[i]];
NSURLRequest* requset = [[NSURLRequest alloc] initWithURL:url];
[self.wkWebView loadRequest:requset];
5.在写cell的滑动刷新时,发现新刷新的cell的具体内容添加的按钮无法触发点击事件,会发现在controller层根本没有给按钮添加点击事件,这时同样需要发送通知同时传值,让controller层接受通知并给这些按钮添加点击事件。
6.对于cell的刷新,我会将scrollView的contentSize多添加一个cell的内容当刷新到最后一个时,就进行网络请求并将请求到的内容添加上去。