标签:
#import "HotNewsTableViewController.h"
#import "HotNewsTableViewCell.h"
#import "HotNews.h"
#import "JudegNetWorkType.h"
#import "HotNewsDetailViewController.h"
#import "MJRefresh.h"
#import "MBProgressHUD+Show.h"
//当前屏幕宽
#define kCurrWith [UIScreen mainScreen].bounds.size.width
@interface HotNewsTableViewController ()
{
BOOL flag;
NSInteger count;
}
@property (nonatomic, retain) NSMutableArray *allDataArray;
@property (nonatomic, retain) NSMutableArray *imgDataArray;
@property (nonatomic, retain) UILabel *label;
@end
@implementation HotNewsTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"今日热点";
//初始化数组
self.allDataArray = [NSMutableArray array];
//设置头View
self.tableView.tableHeaderView = [self setHeadView];
//设置尾View
self.tableView.tableFooterView = [self sendFootView];
[NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(PageControlAction) userInfo:self repeats:YES];
//NSLog(@" kuan : %f",);
/**
* 集成刷新控件
*/
[self setupRefresh];
count = 20;
}
//触发滚动
- (void)PageControlAction
{
if (_allDataArray.count == 0) {
return;
}
//取模型
HotNews *hotNews = _allDataArray[_pageControl.currentPage + 1];
_label.text = hotNews.title;
//判断
if (_pageControl.currentPage != 3) {
//增加当前页
_pageControl.currentPage = _pageControl.currentPage + 1;
[_scrollView setContentOffset:CGPointMake(_pageControl.currentPage * kCurrWith, 0) animated:YES];
return;
} else {
//为3时 -》 置0
_pageControl.currentPage = 0;
_scrollView.contentOffset = CGPointMake(0, 0);
return;
}
}
- (UIView *)setHeadView
{
UIView *allView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, kCurrWith, 170)] autorelease];
_scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kCurrWith, 145)] autorelease];
//可滚动区域
_scrollView.contentSize = CGSizeMake(kCurrWith * 4, CGRectGetHeight(_scrollView.frame));
//添加手势
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];
[_scrollView addGestureRecognizer:tapGR];
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
[allView addSubview:_scrollView];
// 标题
_label = [[[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_scrollView.frame) + 5, kCurrWith, 20)] autorelease];
_label.font = [UIFont systemFontOfSize:13.0f];
[allView addSubview:_label];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title.jpg"]] autorelease];
imageView.frame = CGRectMake(10, 3, 15, 15);
[_label addSubview:imageView];
// 小圆点
_pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(200, 0, 150, 15)] autorelease];
_pageControl.numberOfPages = 4;
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
_pageControl.pageIndicatorTintColor = [UIColor colorWithRed:0.219 green:0.185 blue:1.000 alpha:1.000];
[_pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
[_label addSubview:_pageControl];
return allView;
}
- (UIView *)sendFootView
{
UIView *allView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, kCurrWith, 30)] autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, kCurrWith, 30);
[button setTitle:@"点击加载更多" forState:UIControlStateNormal];
button.alpha = 0.7f;
[button addTarget:self action:@selector(jiazaiButtonAction:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor blueColor];
[allView addSubview:button];
return allView;
}
- (void)jiazaiButtonAction:(UIButton *)sender
{
if ([JudegNetWorkType getNetWorkType] == BadNetWorkLink) {
return;
}
NSString *str = [listUrlOne stringByAppendingString:[NSString stringWithFormat:@"%ld-20.html", (long)count]];
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
__block HotNewsTableViewController *hotNewsTVC = self;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if (dict.count != 0) {
for (NSDictionary *item in dict[@"T1348647909107"]) {
HotNews *hotNews = [HotNews new];
if (![item[@"digest"] isEqualToString:@""]) {
[hotNews setValuesForKeysWithDictionary:item];
[_allDataArray addObject:hotNews];
[hotNews release];
}
}
}
//跟新页面
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
count += 20;
}
//处理轮播图 点击 手势
- (void)tapGRAction:(UITapGestureRecognizer *)sender
{
for (int i = 0; i < 4; i ++) {//i 代表第几个模型
//contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量
if (_scrollView.contentOffset.x == i * kCurrWith) {
HotNewsDetailViewController *hotNewsDetailVC = [[[HotNewsDetailViewController alloc] init] autorelease];
HotNews *test = _allDataArray[i];
hotNewsDetailVC.hotNews = test;
hotNewsDetailVC.docid = test.docid;
[self.navigationController pushViewController:hotNewsDetailVC animated:YES];
}
}
}
////
//- (void)pageControlAction:(UIPageControl *)sender
//{
// [_scrollView setContentOffset:CGPointMake(sender.currentPage * kCurrWith, 0) animated:YES];
// NSLog(@"-------------------");
//}
/**
* 集成刷新控件
*/
- (void)setupRefresh
{
// 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
[self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
[self.tableView headerBeginRefreshing];
// if (_allDataArray.count == 0) {
// [self.tableView headerBeginRefreshing];
// }
// 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
// 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
self.tableView.headerRefreshingText = @"正在帮你刷新中...";
self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
self.tableView.footerRefreshingText = @"正在帮你加载中...";
}
#pragma mark - 发送网络请求,显示数据
- (void)headerRereshing
{
if ([JudegNetWorkType getNetWorkType] == BadNetWorkLink) {
NSLog(@"网络异常,头条数据显示不出来");
} else{
if (_allDataArray.count == 0) {
NSURL *url = [NSURL URLWithString:HotNews_List_URL];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
__block HotNewsTableViewController *hotTVC = self;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if (dict.count != 0) {
for (NSDictionary *item in dict[@"T1348647909107"]) {
HotNews *hotNews = [HotNews new];
if (![item[@"digest"] isEqualToString:@""]) {
[hotNews setValuesForKeysWithDictionary:item];
[_allDataArray addObject:hotNews];
[hotNews release];
}
}
}
//跟新页面
dispatch_async(dispatch_get_main_queue(), ^{
[hotTVC updateDataForUI];
});
}];
} else {
NSURL *url = [NSURL URLWithString:HotNews_List_URL];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
__block HotNewsTableViewController *hotTVC = self;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSMutableArray *newHead = [NSMutableArray array];
if (dict.count != 0) {
for (NSDictionary *item in dict[@"T1348647909107"]) {
HotNews *hotNews = [HotNews new];
if (![item[@"digest"] isEqualToString:@""]) {
[hotNews setValuesForKeysWithDictionary:item];
}
for (int i = 0; i<_allDataArray.count; i++) {
if ([((HotNews *)_allDataArray[i]).title isEqualToString:hotNews.title]) {
flag = YES;
}
}
if (!flag) {
[newHead addObject:hotTVC];
flag = NO;
}
[hotNews release];
}
}
for (HotNews *hotNews in newHead) {
[_allDataArray insertObject:hotNews atIndex:0];
}
//跟新页面
dispatch_async(dispatch_get_main_queue(), ^{
[hotTVC updateDataForUI];
});
}];
}
}
}
#pragma mark 更新页面
- (void)updateDataForUI
{
[self.tableView reloadData];
[self.tableView headerEndRefreshing];
[MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
[MBProgressHUD showSuccessWithText:@"请求成功"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _allDataArray.count - 4;
}
// 显示内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellIdentifier";
HotNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[[HotNewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
if (_allDataArray.count) {
HotNews *hotNews = _allDataArray[indexPath.row + 4];
NSURL *imgUrl = [NSURL URLWithString:hotNews.imgsrc];
[cell.imgsrcView sd_setImageWithURL:imgUrl];
cell.hotNews = hotNews;
}
// 滚动图片新闻
for (int i = 0; i < 4; i++) {
HotNews *hotNews = _allDataArray[i+4];
NSURL *imgUrl = [NSURL URLWithString:hotNews.imgsrc];
UIImageView *imageView = [[UIImageView alloc] init];
[imageView sd_setImageWithURL:imgUrl];
imageView.frame = CGRectMake(CGRectGetWidth(_scrollView.frame) * i, 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame));
[_scrollView addSubview:imageView];
}
HotNews *hotNews = _allDataArray[0];
_label.text = [NSString stringWithFormat:@" %@", hotNews.title];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
HotNewsDetailViewController *hotNewsDetailVC = [[[HotNewsDetailViewController alloc] init] autorelease];
HotNews *test = _allDataArray[indexPath.row + 4];
hotNewsDetailVC.hotNews = test;
hotNewsDetailVC.docid = test.docid;
[self.navigationController pushViewController:hotNewsDetailVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 85;
}
@end
标签:
原文地址:http://www.cnblogs.com/iOS-mt/p/4323482.html