#pragma mark - ViewPagerDataSource
// Tab数量
- (NSUInteger)numberOfTabsForViewPager:(ViewPagerController *)viewPager {
return categoryList.count;
}
// “类别-0”、“类别-1”这些Tab内容添加处
- (UIView *)viewPager:(ViewPagerController *)viewPager viewForTabAtIndex:(NSUInteger)index {
GTCategoryItem *item = [categoryList objectAtIndex:index];
UILabel *label = [UILabel new];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:13.0];
if (item) {
label.text = item.categoryName;
}
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
[label sizeToFit];
return label;
}
// 每个Tab对应的视图控制器(也就是新闻列表页面)
- (UIViewController *)viewPager:(ViewPagerController *)viewPager contentViewControllerForTabAtIndex:(NSUInteger)index {
GTNewsViewController *vCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"NewsViewController"];
[vCtrl setPViewCtrl:self];
return vCtrl;
}
其中,categoryList里存放着多个新闻专题信息的对象,是一个列表。
#pragma mark - ViewPagerDelegate
- (CGFloat)viewPager:(ViewPagerController *)viewPager valueForOption:(ViewPagerOption)option withDefault:(CGFloat)value {
CGFloat result = 0.0;
switch (option) {
case ViewPagerOptionStartFromSecondTab:
result = 1.0;
break;
case ViewPagerOptionCenterCurrentTab:
result = 0.0;
break;
case ViewPagerOptionTabLocation:
result = 1.0;
break;
case ViewPagerOptionTabWidth:
result = self.view.frame.size.width / 5;
break;
default:
result = value;
break;
}
return result;
}
- (UIColor *)viewPager:(ViewPagerController *)viewPager colorForComponent:(ViewPagerComponent)component withDefault:(UIColor *)color {
switch (component) {
case ViewPagerIndicator:
return [[UIColor redColor] colorWithAlphaComponent:0.64];
break;
default:
break;
}
return color;
}
#import "MJRefresh.h"
其次,设置刷新控件。
/**
* 集成刷新控件
*/
- (void)setupRefresh
{
// 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
[self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
// [self.tableView headerBeginRefreshing];
// 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
[self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}
最后,实现顶部刷新(
headerRereshing)和底部刷新(
footerRereshing)方法。
- (void)headerRereshing
{
for (int i = 0; i < 5; i++) {
GTNewsItem *item = [[GTNewsItem alloc] init];
item.newsID = [NSString stringWithFormat:@"%d", i];
item.newsTitle = [NSString stringWithFormat:@"热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻 -- %d", i];
item.newsFrom = @"腾讯新闻";
item.newsPic = nil;
item.timestamp = [[NSDate date] timeIntervalSince1970] * 1000;
item.readCount = i * 1000;
item.commentCount = i * 2000;
[arrNews insertObject:item atIndex:0];
}
[self.tableView reloadData];
[self.tableView headerEndRefreshing];
}
- (void)footerRereshing
{
for (int i = 0; i < 5; i++) {
GTNewsItem *item = [[GTNewsItem alloc] init];
item.newsID = [NSString stringWithFormat:@"%d", i];
item.newsTitle = [NSString stringWithFormat:@"热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新闻热点新 -- %d", i];
item.newsFrom = @"搜狐新闻";
item.newsPic = nil;
item.timestamp = [[NSDate date] timeIntervalSince1970] * 1000;
item.readCount = i * 10;
item.commentCount = i;
[arrNews addObject:item];
}
[self.tableView reloadData];
[self.tableView footerEndRefreshing];
}
其中,arrNews存放着多条新闻消息,两个方法新增消息后刷新一下tableView。
本例子程序里MJRefresh模块源码详见3rd/MJRefresh文件夹。- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = indexPath.row;
UITableViewCell *cell;
if (row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
}
float contentWid = self.view.frame.size.width - 3 * gtNewsPadding;
//定义新的cell
if(cell == nil)
{
if (row == 0) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
ASScroll *asScroll = [[ASScroll alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.width * 0.3)];
asScroll.tag = news_content_ads;
[cell.contentView addSubview:asScroll];
} else {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
// 新闻图片
CGRect picRect = CGRectMake(gtNewsPadding, gtNewsPadding, contentWid / 3, contentWid / 4);
UIImageView *ivPic = [[UIImageView alloc] initWithFrame:picRect];
ivPic.tag = news_content_pic;
[cell.contentView addSubview:ivPic];
// 新闻标题
UILabel *lbTitle = [[UILabel alloc] init];
lbTitle.font = [UIFont boldSystemFontOfSize:gtFontSizeBig];
lbTitle.tag = news_content_title;
lbTitle.textColor = [UIColor brownColor];
[cell.contentView addSubview:lbTitle];
// 新闻来源
UILabel *lbFrom = [[UILabel alloc] init];
lbFrom.font = [UIFont boldSystemFontOfSize:gtFontSizeSmall];
lbFrom.tag = news_content_from;
lbFrom.textColor = [UIColor grayColor];
[cell.contentView addSubview:lbFrom];
// 新闻阅读数量(图标)
UIImageView *ivReadCount = [[UIImageView alloc] init];
ivReadCount.tag = news_content_read_img;
[cell.contentView addSubview:ivReadCount];
// 新闻阅读数量
UILabel *lbReadCount = [[UILabel alloc] init];
lbReadCount.font = [UIFont boldSystemFontOfSize:gtFontSizeSmall];
lbReadCount.tag = news_content_read_count;
lbReadCount.textColor = [UIColor grayColor];
[cell.contentView addSubview:lbReadCount];
// 评论数量(图片)
UIImageView *ivCommentCount = [[UIImageView alloc] init];
ivCommentCount.tag = news_content_comment_img;
[cell.contentView addSubview:ivCommentCount];
// 评论数量
UILabel *lbCommentCount = [[UILabel alloc] init];
lbCommentCount.font = [UIFont boldSystemFontOfSize:gtFontSizeSmall];
lbCommentCount.tag = news_content_comment_count;
lbCommentCount.textColor = [UIColor grayColor];
[cell.contentView addSubview:lbCommentCount];
}
}
if (row == 0) {
ASScroll *asScroll = (ASScroll *)[cell.contentView viewWithTag:news_content_ads];
NSMutableArray * imagesArray = [[NSMutableArray alloc] init];
int noOfImages = 3 ;
for (int imageCount = 0; imageCount < noOfImages; imageCount++)
{
[imagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"ad_%d.jpg",imageCount+1]]];
}
[asScroll setArrOfImages:imagesArray];
} else {
GTNewsItem *obj = [arrNews objectAtIndex:(row - 1)];
float fHei = gtNewsPadding;
// 新闻图片
UIImageView *ivPic = (UIImageView *)[cell.contentView viewWithTag:news_content_pic];
ivPic.image = [UIImage imageNamed:@"placeholder_logo.png"];
// 新闻标题
UILabel *lbTitle = (UILabel *)[cell.contentView viewWithTag:news_content_title];
CGSize ttlSize = {0, 0};
ttlSize = [obj.newsTitle sizeWithFont:[UIFont systemFontOfSize:gtFontSizeBig]
constrainedToSize:CGSizeMake(2 * contentWid / 3, 5000)
lineBreakMode:NSLineBreakByWordWrapping];
lbTitle.numberOfLines = 0; //表示label可以多行显示
lbTitle.lineBreakMode = NSLineBreakByWordWrapping; //换行模式
CGRect ttlRect = CGRectMake(contentWid / 3 + 2 * gtNewsPadding, fHei, 2 * contentWid / 3, ttlSize.height);
[lbTitle setFrame:ttlRect];
lbTitle.text = obj.newsTitle;
fHei += ttlSize.height + gtNewsPadding;
// 控件位置 从右算起
float widPos = self.view.frame.size.width - gtNewsPadding - 104;
// 新闻来源
UILabel *lbFrom = (UILabel *)[cell.contentView viewWithTag:news_content_from];
float fromWid = widPos - (contentWid / 3 + 2 * gtNewsPadding + 4);
CGRect fromRect;
if (fromWid > 0) {
fromRect = CGRectMake(contentWid / 3 + 2 * gtNewsPadding, fHei, fromWid, 16);
} else {
fromRect = CGRectMake(contentWid / 3 + 2 * gtNewsPadding, fHei, 0, 16);
}
[lbFrom setFrame:fromRect];
lbFrom.text = obj.newsFrom;
lbFrom.textAlignment = NSTextAlignmentLeft;
// 阅读数量
UILabel *rcLabel = (UILabel *)[cell.contentView viewWithTag:news_content_read_count];
CGRect rcLRect = CGRectMake(widPos, fHei, 30, 16);
[rcLabel setFrame:rcLRect];
rcLabel.text = [NSString stringWithFormat:@"%ld",obj.readCount];
rcLabel.textAlignment = NSTextAlignmentRight;
widPos += 30 + 4;
// 阅读数量(图标)
UIImageView *ivReadCount = (UIImageView *)[cell.contentView viewWithTag:news_content_read_img];
CGRect rcRect = CGRectMake(widPos, fHei, 16, 16);
[ivReadCount setFrame:rcRect];
ivReadCount.image = [UIImage imageNamed:@"icon_read_count.png"];
widPos += 16 + 4;
// 评论数量
UILabel *ccLabel = (UILabel *)[cell.contentView viewWithTag:news_content_comment_count];
CGRect ccLRect = CGRectMake(widPos, fHei, 30, 16);
ccLabel.text = [NSString stringWithFormat:@"%ld",obj.commentCount];
ccLabel.textAlignment = NSTextAlignmentRight;
[ccLabel setFrame:ccLRect];
widPos += 30 + 4;
// 评论数量(图标)
UIImageView *ivCommentCount = (UIImageView *)[cell.contentView viewWithTag:news_content_comment_img];
CGRect ccRect = CGRectMake(widPos, fHei, 16, 16);
[ivCommentCount setFrame:ccRect];
ivCommentCount.image = [UIImage imageNamed:@"icon_comment_count.png"];
}
return cell;
}
static NSString *CellIdentifier1 = @"NewsAdsCell";
static NSString *CellIdentifier2 = @"NewsListCell";
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
bundle: nil];
// 左侧菜单视图控制器
GTMenuViewController *leftMenu = [mainStoryboard instantiateViewControllerWithIdentifier:@"MenuViewController"];
leftMenu.view.backgroundColor = [UIColor lightGrayColor];
// 无右侧菜单
[SlideNavigationController sharedInstance].righMenu = nil;
// 设置左侧菜单
[SlideNavigationController sharedInstance].leftMenu = leftMenu;
return YES;
}
#pragma mark - SlideNavigationController Methods
// 可以显示左侧菜单
- (BOOL)slideNavigationControllerShouldDisplayLeftMenu
{
return YES;
}
// 不显示右侧菜单
- (BOOL)slideNavigationControllerShouldDisplayRightMenu
{
return NO;
}
设置菜单控制器GTMenuViewController对象的父类控制器,用于页面跳转。这个很重要!因为GTMenuViewController类本身没有
navigationController,所以不能跳转页面。可以在GTMainViewController的viewDidLoad方法里初始化。
- (void)viewDidLoad
{
...
GTMenuViewController *lMenu = (GTMenuViewController *)[SlideNavigationController sharedInstance].leftMenu;
[lMenu setPViewCtrl:self];
...
[super viewDidLoad];
}