仿网易新闻界面
#import "AppDelegate.h"
#import "ViewController.h"
@interfaceAppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]init];
self.window.frame = [[UIScreenmainScreen]bounds];
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
ViewController *vc = [[ViewControlleralloc]init];
vc.title = @"今日头条";
UINavigationController *nv = [[UINavigationControlleralloc]initWithRootViewController:vc];
self.window.rootViewController = nv;
// Override point for customization after application launch.
returnYES;
}
#import <UIKit/UIKit.h>
externint k,p ;//k是获取tabel哪一行被点击 p是获取哪一个tabel被点击
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "MyTableViewCell.h"
#import "New.h"
#import "DataViewController.h"
NSArray *array;
@interfaceViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
UISegmentedControl *seg;
UIScrollView *scroll;
UITableView *tabel;
NSArray *array0;
NSArray *array1;
NSArray *array2;
NSArray *array3;
UIScrollView *scr;
NSInteger index;
}
@end
@implementation ViewController
int k,p=0,n,x,y;
- (void)viewDidLoad {
[superviewDidLoad];
New *ne = [[New alloc]init];
array0 = [ne domesticdata ];//获取国内新闻对象
New *ne1 = [[New alloc]init];
array1 = [ne1 internationaldata ];//获取国际新闻对象
New *ne2 = [[New alloc]init];
array2 = [ne2 socialdata ];//获取社会新闻对象
New *ne3 = [[New alloc]init];
array3 = [ne3 entertainmentdata ];//获取娱乐新闻对象
[selfperformSelector:@selector(event) withObject:selfafterDelay:1];
/*************************UISegmentedControl************************/
seg = [[UISegmentedControlalloc]initWithItems:@[@"国内",@"国际",@"社会",@"娱乐"]];//初始化seg,并给seg分段
seg.backgroundColor = [UIColorwhiteColor];
seg.frame = CGRectMake(-5, 64, 385, 40);//设置seg的位置与大小
seg.selectedSegmentIndex = 0;
[segaddTarget:selfaction:@selector(segmen:) forControlEvents:UIControlEventValueChanged];//seg的监听事件
[self.view addSubview:seg];
/*************************UISegmentedControl************************/
/*************************UIScrollView******************************/
scroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(0, 104, 375, 560)];//scroll的位置与大小
scroll.contentSize = CGSizeMake(376*4, 0);//设置scroll可以左右滑动,滑动四个屏幕大小的视图、、
scroll.backgroundColor = [UIColorredColor];
scroll.pagingEnabled = YES;//翻整页
scroll.bounces = NO;//取消回弹效果
scroll.delegate = self;
[self.view addSubview:scroll];
/*************************UIScrollView******************************/
//连续创建4个tabelview,放在scroll上
for (int j =0; j<4; j++) {
scr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 250)];
scr.contentSize = CGSizeMake(375*6, 0);
scr.pagingEnabled = YES;
scr.tag = j+5;
scr.delegate = self;
scr.bounces = NO;
/*************************UIImageView*******************************/
for (int i=0; i<6; i++) {
UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(375*i, 0,375, 250)];//设置imageview的位置与大小
imgview.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1+j*6]];//给imageview添加图片
[scr addSubview:imgview];
}
/*************************UIImageView*******************************/
/*************************UITableView*******************************/
tabel = [[UITableViewalloc]initWithFrame:CGRectMake(375*j, 0, 375, 570) style:UITableViewStylePlain];
tabel.rowHeight = 130;//设置每个cell的高
tabel.delegate = self;
tabel.tableHeaderView = scr;//给tabel的顶视图添加控件
tabel.tag = j;//给tabel添加标记,方便以后使用tabel
tabel.dataSource = self;
[scroll addSubview:tabel];
/*************************UITableView*******************************/
}
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView.tag ==0)return array0.count;
if(tableView.tag ==1)return array1.count;
if(tableView.tag ==2)return array2.count;
return array3.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag ==0) array = array0;
if (tableView.tag ==1) array = array1;
if (tableView.tag ==2) array = array2;
if (tableView.tag ==3) array = array3;
static NSString *iden = @"cell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
if (cell == nil) {
cell = [[MyTableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];
}
New *info =array[indexPath.row];
cell.titlelabel.text = info.title;
cell.subtitlelabel.text = info.subtitle;
cell.imageview.image = [UIImage imageNamed:info.image];
return cell;
}
-(void) segmen:(UISegmentedControl *) sg
{
CGPoint p = {seg.selectedSegmentIndex*375,0};//获得scroll的坐标
[scrollsetContentOffset:p];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGPoint p =scroll.contentOffset;
seg.selectedSegmentIndex = p.x/375;//获得seg的当前段
y++;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
k =indexPath.row;
p = index;
NSLog(@"--------%zi",p);
[tableView deselectRowAtIndexPath:indexPath animated:YES];//tabel被选中后,立马取消选中
[self.navigationControllerpushViewController:[[DataViewControlleralloc] init] animated:YES];
NSLog(@"%d",p);
NSLog(@"%d",k);
}
-(void) event
{
if( seg.selectedSegmentIndex ==0)n=5;
if( seg.selectedSegmentIndex ==1)n=6;
if( seg.selectedSegmentIndex ==2)n=7;
if( seg.selectedSegmentIndex ==3)n=8;
UIScrollView *s = (UIScrollView *)[self.viewviewWithTag:n];//拿出被标记的scroll
CGPoint p = s.contentOffset;//当前scroll的坐标
int m =p.x+375;
x++;
CGPoint q ={m,0};
if (x>5-y) {
x=0;
q.x=0;y=0;
[s setContentOffset:p];
}
[s setContentOffset:q];
[selfperformSelector:@selector(event) withObject:selfafterDelay:1];//延时的设定
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
@property (nonatomic ,strong) UIImageView *imageview;
@property (nonatomic,strong) UILabel *titlelabel;
@property (nonatomic ,strong) UILabel *subtitlelabel;
@end
#import "MyTableViewCell.h"
@implementation MyTableViewCell
-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_imageview = [[UIImageViewalloc]initWithFrame:CGRectMake(260, 15, 100, 100)];
[self addSubview:_imageview];
_titlelabel = [[UILabelalloc]initWithFrame:CGRectMake(10, 10, 200, 50)];
_titlelabel.font = [UIFont systemFontOfSize:20];
_titlelabel.numberOfLines =0;
_titlelabel.textColor = [UIColor blackColor];
[self addSubview:_titlelabel];
_subtitlelabel = [[UILabelalloc]initWithFrame:CGRectMake(10, 85, 200, 30)];
_subtitlelabel.font = [UIFont systemFontOfSize:15];
[selfaddSubview:_subtitlelabel];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
#import <Foundation/Foundation.h>
@interface New : NSObject
@property (nonatomic, copy) NSString *image;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;
-(NSArray *) domesticdata;
-(NSArray *) internationaldata;
-(NSArray *) socialdata;
-(NSArray *) entertainmentdata;
@end
#import "New.h"
@implementation New
-(NSArray *) domesticdata
{
New *info1 = [[New alloc]init];
info1.image = @"1.png";
info1.title = @"全国政协原副主席苏荣女婿落马 被指胆子小";
info1.subtitle = @"湛江新闻网";
;
New *info2 = [[New alloc]init];
info2.image = @"3.png";
info2.title = @"31省份前三季度GDP广东居首 各地之和超全国约1.9万亿";
info2.subtitle = @"中国新闻网";
New *info3 = [[New alloc]init];
info3.image = @"2";
info3.title = @"财经年会2016全会二:国内经济解“困”与增长应对(组图) (9/9)";
info3.subtitle = @"和讯网";
New *info4 = [[New alloc]init];
info4.image = @"5";
info4.title = @"内蒙古东北等局地有暴雪 南方阴雨绵绵";
info4.subtitle = @"中国天气网";
NSArray *array = @[info1,info2,info3,info4];
NSLog(@"%@",array);
return array;
}
-(NSArray *) internationaldata
{
New *info1 = [[New alloc]init];
info1.image = @"6.png";
info1.title = @"11月17日全球媒体头条速览:情报显示IS曾计划绑架马来西亚领导人";
info1.subtitle = @"中国日报网";
;
New *info2 = [[New alloc]init];
info2.image = @"11.jpeg";
info2.title = @"巴黎恐袭嫌犯抓捕行动结束 组织头目下落仍不明";
info2.subtitle = @"中国新闻网";
New *info3 = [[New alloc]init];
info3.image = @"12.jpeg";
info3.title = @"美媒:遭炸弹威胁法航2架客机均未发现爆炸物";
info3.subtitle = @"和讯网";
New *info4 = [[New alloc]init];
info4.image = @"13.jpeg";
info4.title = @"日掀反安倍狂潮 称安倍下台系防止恐袭上策";
info4.subtitle = @"环球时报-环球网(北京)";
NSArray *array = @[info1,info2,info3,info4];
NSLog(@"%@",array);
return array;
}
-(NSArray *) socialdata
{
New *info1 = [[New alloc]init];
info1.image = @"15.jpeg";
info1.title = @"男子手术单现子宫切除费用";
info1.subtitle = @"中国新闻网";
New *info2 = [[New alloc]init];
info2.image = @"16.jpeg";
info2.title = @"重庆今年已建成700多座旅游厕所";
info2.subtitle = @"中国日报网";
New *info3 = [[New alloc]init];
info3.image = @"17.jpeg";
info3.title = @"司机未拔车钥匙 小偷盗走大巴车";
info3.subtitle = @"和讯网";
New *info4 = [[New alloc]init];
info4.image = @"18.jpeg";
info4.title = @"80后小伙单骑骑行“丝绸之路";
info4.subtitle = @"中国日报网";
NSArray *array = @[info1,info2,info3,info4];
NSLog(@"%@",array);
return array;
}
-(NSArray *) entertainmentdata
{
New *info1 = [[New alloc]init];
info1.image = @"19.jpeg";
info1.title = @"传杨幂刘恺威离婚 杨幂李易峰亲密";
info1.subtitle = @"国际在线网";
New *info2 = [[New alloc]init];
info2.image = @"20.jpeg";
info2.title = @"郑爽身材暴瘦再秀竹竿腿";
info2.subtitle = @"娱乐网";
New *info3 = [[New alloc]init];
info3.image = @"17.jpeg";
info3.title = @"司机未拔车钥匙 小偷盗走大巴车";
info3.subtitle = @"和讯网";
New *info4 = [[New alloc]init];
info4.image = @"21.jpeg";
info4.title = @"60岁赵雅芝现身敬老院 与同龄人互动";
info4.subtitle = @"娱乐网";
NSArray *array = @[info1,info2,info3,info4];
NSLog(@"%@",array);
return array;
}
@end