美文网首页
2018-06-27

2018-06-27

作者: 鉲佈奇言 | 来源:发表于2018-06-27 14:13 被阅读0次

    #import "ViewController.h"
    #import "SDImageCache.h"
    #import "UIImageView+WebCache.h"
    #import "News.h"
    #import "detailViewController.h"

    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
       
        NSMutableArray *tableDataArr;//给表格赋值的数组
    }
    @property(nonatomic,strong)UITableView *table;
    @property(nonatomic,strong)DataBase *da;

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
       
        self.table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        self.table.delegate=self;
        self.table.rowHeight = 80;
        self.table.dataSource=self;
        [self.view addSubview:_table];
       
       
        id url=@"http://v.juhe.cn/weixin/query?";
        NSDictionary *dic=@{@"key":@"57262e632820d778937ea674751f4872"};
       
       
       
        _da = [[DataBase alloc] init];
        //使用get请求方式获取数据
        [_da getRequestWithURLString:url params:dic success:^(NSURLSessionDataTask *task, id responseObject) {
           
           
            tableDataArr = [[NSMutableArray alloc] init];
            for (NSDictionary  *dic in responseObject[@"result"][@"list"]) {
               
                News *model = [[News alloc] init];
                model.title = dic[@"title"];
                model.weburl = dic[@"url"];
                model.pic = dic[@"firstImg"];
               
                [tableDataArr addObject:model];
                NSLog(@"%@=======",tableDataArr);
            }
           
            [_table reloadData];
           
           
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
           
            NSLog(@"error: %@",error);
           
        }];
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return tableDataArr.count;
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell=nil;
        static NSString *reuse=@"cell";
        if (cell==nil) {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }else{
            while ([cell.contentView.subviews lastObject] != nil) {
                [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
            }
        }

       
        News *new = tableDataArr[indexPath.row];

        //获取cell对应的News对象
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];
        [img  sd_setImageWithURL:
         [NSURL URLWithString:new.pic] placeholderImage:[UIImage imageNamed:@"1"]];
        [cell addSubview:img];
       
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, self.view.frame.size.width-60, 44)];
        lable.text = new.title;
        lable.numberOfLines = 0;
        [cell addSubview:lable];
       
        return cell;
    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       
        News *one=[tableDataArr objectAtIndex:indexPath.row];
        detailViewController *detail = [[detailViewController alloc]init];
        detail.url = one.weburl;
        [self.navigationController pushViewController:detail animated:YES];

    }

    相关文章

      网友评论

          本文标题:2018-06-27

          本文链接:https://www.haomeiwen.com/subject/tmvbyftx.html