使用了mvc模式和manage封装网络请求,
使用了tableView,在tableview中添加scrollView实现无线轮播图,实现无线轮播图的方法与share项目的方法一致,值得注意的一点是,关于添加在scrollView的东西是不支持使用masonry的。
使用MVC模式,将tableview写在view内
首先说说,顶部的日期如何处理
调用系统内部时间,将其赋值给label
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger Flags = NSCalendarUnitYear |NSCalendarUnitMonth |NSCalendarUnitDay |NSCalendarUnitWeekday |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
首先肯定是先创建一个滚动视图,在该滚动视图上添加图片,如何实现“无限”,在经过一些资料的查询后,我给出了以下解决方案
比如说我要添加4张图分别为1,2,3,4号,但在程序的实现中不能只添加4张图,不然当我拖到4张图的后面的时候我会发现4号的后面不是1号图,而是我为滚动视图设置的背景色,那其实我是需要设置6张图来达成4张图的无限循环图
我们需要将这4张图这样排序
4号,1号,2号,3号,4号,1号
图片设置完后,设置一个定时器,然后自动去滚动就好
ScrollerViewTableViewCell* mainCell = [self.mainTableView dequeueReusableCellWithIdentifier:@"scrollCell" forIndexPath:indexPath];
mainCell.backgroundColor = [UIColor clearColor];
NSString* top = [[NSString alloc] init];
top = _viewDictionary[@"top_stories"][4][@"image"];
NSURL* topImageUrl = [NSURL URLWithString:top];
UIImage* topImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:topImageUrl]];
UIImageView* topImageView = [[UIImageView alloc] initWithImage:topImage];
topImageView.frame = CGRectMake(Width*(0), 0, Width, Width);
[mainCell.infScrollView addSubview:topImageView];
UILabel* titleTopLabel = [[UILabel alloc] init];
titleTopLabel.textColor = [UIColor whiteColor];
[titleTopLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:26]];
titleTopLabel.textAlignment = NSTextAlignmentLeft;
titleTopLabel.lineBreakMode = NSLineBreakByCharWrapping;
titleTopLabel.numberOfLines = 0;
titleTopLabel.text = _viewDictionary[@"top_stories"][4][@"title"];
titleTopLabel.frame = CGRectMake(Width*(0) + 20,300, Width, 100);
[mainCell.infScrollView addSubview:titleTopLabel];
UILabel* hintTopLabel = [[UILabel alloc] init];
[hintTopLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
hintTopLabel.textColor = [UIColor colorWithWhite:0.9 alpha:1];
hintTopLabel.text = _viewDictionary[@"top_stories"][4][@"hint"];
hintTopLabel.frame = CGRectMake(Width*(0)+20, 380, Width-40, 30);
[mainCell.infScrollView addSubview:hintTopLabel];
for (int i = 0; i < 5; i++) {
NSString* pictureName = [[NSString alloc] init];
pictureName = _viewDictionary[@"top_stories"][i][@"image"];
NSURL* imageUrl = [NSURL URLWithString:pictureName];
UIImage* mainImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
UIImageView* mainImgView = [[UIImageView alloc] initWithImage:mainImg];
mainImgView.frame = CGRectMake(Width*(i+1), 0, Width, Width);
[mainCell.infScrollView addSubview:mainImgView];
UILabel* titleLabel = [[UILabel alloc] init];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:26]];
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
titleLabel.numberOfLines = 0;
titleLabel.text = _viewDictionary[@"top_stories"][i][@"title"];
titleLabel.frame = CGRectMake(Width*(i+1) + 20,300, Width-40, 100);
[mainCell.infScrollView addSubview:titleLabel];
UILabel* hintLabel = [[UILabel alloc] init];
[hintLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
hintLabel.textColor = [UIColor colorWithWhite:0.9 alpha:1];
hintLabel.text = _viewDictionary[@"top_stories"][i][@"hint"];
hintLabel.frame = CGRectMake(Width*(i+1) + 20, 380, Width-40, 30);
[mainCell.infScrollView addSubview:hintLabel];
}
NSString* bottom = [[NSString alloc] init];
bottom = _viewDictionary[@"top_stories"][0][@"image"];
NSURL* bottomImageUrl = [NSURL URLWithString:bottom];
UIImage* bottomImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:bottomImageUrl]];
UIImageView* bottomImageView = [[UIImageView alloc] initWithImage:bottomImage];
bottomImageView.frame = CGRectMake(Width*(6), 0, Width, Width);
[mainCell.infScrollView addSubview:bottomImageView];
UILabel* titleBottomLabel = [[UILabel alloc] init];
titleBottomLabel.textColor = [UIColor whiteColor];
[titleBottomLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:26]];
titleBottomLabel.textAlignment = NSTextAlignmentLeft;
titleBottomLabel.lineBreakMode = NSLineBreakByCharWrapping;
titleBottomLabel.numberOfLines = 0;
titleBottomLabel.text = _viewDictionary[@"top_stories"][0][@"title"];
titleBottomLabel.frame = CGRectMake(Width*(6)+20,300, Width-40, 100);
[mainCell.infScrollView addSubview:titleBottomLabel];
UILabel* hintBottomLabel = [[UILabel alloc] init];
[hintBottomLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
hintBottomLabel.textColor = [UIColor colorWithWhite:0.9 alpha:1];
hintBottomLabel.text = _viewDictionary[@"top_stories"][0][@"hint"];
hintBottomLabel.frame = CGRectMake(Width*(6)+20, 380, Width-40, 30);
[mainCell.infScrollView addSubview:hintBottomLabel];
这里是请求第一天的数据,为请求到的数据设置模版使用cell
一张图片和两个label,因为标题可能会有很多字,这样子会超过两行,所以我们需要设置一个限制
请求到的数据赋给模版cell
MainPageTableViewCell* mainCell = [self.mainTableView dequeueReusableCellWithIdentifier:@"mainCell" forIndexPath:indexPath];
mainCell.backgroundColor = [UIColor whiteColor];
mainCell.titleLabel.text =_viewBeforeDictionary[@"stories"][indexPath.row%7-1][@"title"];
NSString* imageString = [[NSString alloc] init];
imageString = self.viewBeforeDictionary[@"stories"][indexPath.row%7-1][@"images"][0];
NSURL* imageUrl = [NSURL URLWithString:imageString];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
mainCell.titleImageView.image = image;
[mainCell.contentView addSubview:mainCell.titleImageView];
mainCell.msgLabel.text = _viewBeforeDictionary[@"stories"][indexPath.row%7-1][@"hint"];
return mainCell;
每一天和每一天之间都有间隔,这里选择使用一个自定义cell来实现
使用masonry进行布局
由于默认运行机制,cell会在网络请求完毕之前完成创建,这样就会导致没有cell为空,这是一个关于线程的问题,我们只需要在完成网络请求之后再进行cell的创建就可以保证cell不为空。
具体实现方法如下:
dispatch_async(dispatch_get_main_queue(), ^{
[self.mainview viewInit];
});
重写视图初始化方法,将其在请求后进行视图创建。