#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
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
- UIView+SGFrame.h #import @interface UIView (SGFrame) @pro...