iOS--知乎日报第二周总结

本文讲述了iOS开发中遇到的问题,如WKWebview加载网页时间长、cell点击后的页面加载优化、Masonry布局限制、导航栏隐藏、滚动事件与新闻更新的异步加载,以及工具栏的添加和注意事项。
摘要由CSDN通过智能技术生成

前言

本周是知乎日报的第二周,这几天写的东西挺少的 ;
目前大概实现了新闻的点击进入,以及轮播图的点击进入 ,还有新闻进入时的异步加载 (但更新时未能实现) ;

遇到的问题

cell点击进入网页

这里用到了iOS自带的控件WKWebview ;老版本的话还可以用UIwebview,但现在基本用不了 ;
原理:在cell的点击函数中用push方法进入下一个MVC,计算已有新闻数据创建获取scrollview,一个新闻对应一个WKWebview对象 ;
WKWebview控件的话我只是简单了解了一下,毕竟暂时只用到了其中的一个属性方法,可以看看别人的
iOS (一) - UIWebView 与 WKWebView . 基本使用

cell第一次进入的时间太长

原因是一开始我是先在Viewdidload方法中创建已有的新闻数据对应的WKwebiew,但加载WKwebview加载网页的时候要的时间比较长,加载的越多,进入的时间越长 ;比如我在循环创建多个wkwebview的同时,要先加载第一个网页,然后依此加载,这里学长告诉我要用到异步加载,即只加载第一个点入的网页,在网页显示后再加载其他的网页 ;
简单的了解了GCd的异步加载后,据我所知的是我可以利用异步加载,在主线程外后台同时请求新闻网页的数据,但不能在后台实现控件操作,所以在Viewdidappea中才实现其他新闻页的[webview loadRequest:request] ;

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    newsTableViewCell* cell = [self.mView.tableview dequeueReusableCellWithIdentifier:@"0"] ;
    if (cell == nil) {
        cell = [[newsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"0"] ;
    }
    cell.scrollview.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * self.mModel.mmModel.newsarray.count, [UIScreen mainScreen].bounds.size.height) ;
    cell.scrollview.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width * (self.index), 0) ;
    cell.scrollview.tag = 101 ;
    cell.scrollview.delegate = self ;
    for (int i = 0; i < self.mModel.mmModel.newsarray.count; i++) {
        
            WKWebView* webview = [[WKWebView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 100)] ;
            [self.mModel.wkwebviewarray addObject:webview] ;
            [cell.scrollview addSubview:webview] ;
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                for (int i = 0; i < self.mModel.mmModel.newsarray.count; i++) {
                    NSString* urlstring = [NSString stringWithFormat:@"https://daily.zhihu.com/story/%@",self.mModel.mmModel.newsarray[i][@"id"]] ;
                    NSURL* url = [NSURL URLWithString:urlstring] ;
                    NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
                    [self.mModel.requestarray addObject:request] ;
                }
                
            }) ;
//            [webview loadRequest:request] ;
        
    }
    NSString* urlstring = [NSString stringWithFormat:@"https://daily.zhihu.com/story/%@",self.mModel.mmModel.newsarray[self.index][@"id"]] ;
    NSURL* url = [NSURL URLWithString:urlstring] ;
    NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
    WKWebView* webview01 = self.mModel.wkwebviewarray[self.index] ;
    [webview01 loadRequest:request] ;
    return cell;
}

- (void)viewDidAppear:(BOOL)animated {
    for (int i = 0; i < self.mModel.mmModel.newsarray.count; i++) {
        if (i != self.index) {
            WKWebView* webview = self.mModel.wkwebviewarray[i] ;
            NSURLRequest* request = self.mModel.requestarray[i] ;
            [webview loadRequest:request] ;
        }
    }
}

对scrollview使用Masonry

当对scrollview的某个控件施加Masonry约束时,会发现跟自己想的不太一样,其实masnry施加的布局约束是对contensize的 ;

导航栏的隐藏

这里只要改变一下属性的值就行了;
[self.navigationController setNavigationBarHidden:YES animated:YES] ;

还有就是要注意一下wkwebview不能用masonry布局 ;

新闻内容更新时的异步加载

这时的异步加载的话只能在滑动过程中实行,但为了连续滑动不出现白屏的现象,我们必须提前加载,所以我就进行了比较简单的处理;

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.tag == 101) {
        int page = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width ;
        self.index = page ;
        if (page >= self.mModel.mmModel.newsarray.count - 5) {
            //请求新的数据
            self.mModel.mmModel.days++ ;
            self.mModel.mmModel.date = [[NSDate alloc] initWithTimeIntervalSinceNow: self.mModel.mmModel.days * -3600 * 24] ;
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init] ;
            [formatter setDateFormat:@"yyyyMMdd"] ;
            NSString* datestr = [formatter stringFromDate:self.mModel.mmModel.date] ;
            NSString* urlstr = [NSString stringWithFormat:@"https://news-at.zhihu.com/api/4/news/before/%@",datestr] ;
            
            NSURL* url = [NSURL URLWithString:urlstr] ;
            //刷新时进行新的网络请求
            [[Manager shareSingleton] NetWorkGetDataAgain:^(TestModel * _Nullable mainModel) {
                NSDictionary* adict = [mainModel toDictionary] ;
                [self.mModel.mmModel.newsarray addObjectsFromArray:adict[@"stories"]] ;
                self.mModel.mmModel.datacount += 1 ;
                scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * self.mModel.mmModel.newsarray.count, [UIScreen mainScreen].bounds.size.height) ;
                for (int i = (int)self.mModel.mmModel.newsarray.count - 5; i < self.mModel.mmModel.newsarray.count; i++) {
                    WKWebView* webview = [[WKWebView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 100)] ;
                    [scrollView addSubview:webview] ;
                    NSString* urlstring = [NSString stringWithFormat:@"https://daily.zhihu.com/story/%@",self.mModel.mmModel.newsarray[i][@"id"]] ;
                    NSURL* url = [NSURL URLWithString:urlstring] ;
                    NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
                    [webview loadRequest:request] ;
                    
                }
                
            } andError:^(NSError * _Nullable error) {
                NSLog(@"Error: %@",error) ;
            } andURL:url] ;
        }
        
    }
}

工具栏的添加

这个项目应该是学了toolbar以来第一次用到,toolbar默认是隐藏的,所以要
self.navigationController.toolbarHidden = NO ;
self.navigationController.toolbar.translucent = NO ;
添加按钮的话 ;

 UIButton* btn01 = [UIButton buttonWithType:UIButtonTypeCustom] ;
    [btn01 setImage:[UIImage imageNamed:@"fanhui-2.png"] forState:UIControlStateNormal] ;
    btn01.frame = CGRectMake(0, 0, 30, 30) ;
    [btn01 addTarget:self action:@selector(pressfanhui) forControlEvents:UIControlEventTouchUpInside] ;
    UIBarButtonItem* btnfanhui = [[UIBarButtonItem alloc] initWithCustomView:btn01] ;
    
    
    UIImageView* imageviewkline = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vertical_line.png"]] ;
    imageviewkline.frame = CGRectMake(0, 0, 10, 10) ;
    UIBarButtonItem* btnline = [[UIBarButtonItem alloc] initWithCustomView:imageviewkline] ;
    
    
    UIBarButtonItem* btnf01 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil] ;
    btnf01.width = 20 ;

    UIImageView* imageviewpinglun = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pinglun.png"]] ;
    imageviewpinglun.frame = CGRectMake(0, 10, 30, 30) ;
    UILabel* labelping = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 20, 20)] ;
    labelping.text = @"5" ;
    UIView* viewping = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)] ;
    [viewping addSubview:imageviewpinglun] ;
    [viewping addSubview:labelping] ;
    UIBarButtonItem* btnpinglun = [[UIBarButtonItem alloc] initWithCustomView:viewping] ;
    btnpinglun.width = 60 ;
    
    UIBarButtonItem* btnf02 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil] ;
    btnf02.width = 20 ;
    
    UIImageView* imageviewzan = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dianzan.png"]] ;
    imageviewzan.frame = CGRectMake(10, 10, 30, 30) ;
    UILabel* labelzan = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 20, 20)] ;
    labelzan.text = @"5" ;
    UIView* viewzan = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)] ;
    [viewzan addSubview:imageviewzan] ;
    [viewzan addSubview:labelzan] ;
    UIBarButtonItem* btnzan = [[UIBarButtonItem alloc] initWithCustomView:viewzan] ;
    btnzan.width = 60 ;
    
    UIBarButtonItem* btnf03 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil] ;
    btnf03.width = 40 ;
    
    UIImageView* imageviewshoucang = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shoucang-3.png"]] ;
    imageviewshoucang.frame = CGRectMake(0, 0, 10, 10) ;
    UIBarButtonItem* btnshoucang = [[UIBarButtonItem alloc] initWithCustomView:imageviewshoucang] ;
    
    UIBarButtonItem* btnf04 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil] ;
    btnf04.width = 40 ;
    
    UIImageView* imageviewpost = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shangchuan-2.png"]] ;
    imageviewpost.frame = CGRectMake(0, 0, 10, 10) ;
    UIBarButtonItem* btnpost = [[UIBarButtonItem alloc] initWithCustomView:imageviewpost] ;
    
    NSArray* btnarray = [NSArray arrayWithObjects:btnfanhui,btnline, btnf01, btnpinglun,btnf02, btnzan, btnf03, btnshoucang, btnf04, btnpost,  nil] ;
    self.toolbarItems = btnarray ;

注意一下,上面要点击响应的按钮,应该在上面添加一个UIbutton,应为如果为Uibarbutton可能被上面的Ui控件盖住无法响应 ;

以及一部分占位按钮 ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值