仿新闻顶部滚动

#import "informationViewController.h"

#import "informationView/informationViewCell.h"

#define topButtonWidth100

@interface informationViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>{

    UIScrollView*_topScrollView;


    NSMutableArray*_mArrayTopButton;

    NSMutableArray*_mArrayTitle;

}

@property(nonatomic,strong)UITableView *tableView;

@end

@implementationinformationViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.hidden = YES;

    self.view.backgroundColor = [UIColor whiteColor];

    _mArrayTitle = [NSMutableArray array];

    _mArrayTopButton = [NSMutableArray array];


    [self setTopScrollView];


    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, StatusBarHeight+40, self.view.frame.size.width, self.view.frame.size.height-StatusBarHeight-40)];

    _tableView.delegate = self;

    _tableView.dataSource = self;

    [self.view addSubview:_tableView];

    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    _tableView.tableFooterView = [[UIView alloc]init];

}

-(void)setTopScrollView{


     _mArrayTitle = [NSMutableArray arrayWithObjects:@"期货要闻",@"综合资讯",@"今日导读",@"内盘播报",nil];

    _topScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(12, StatusBarHeight, WIDTH-24, 40)];


    _topScrollView.showsHorizontalScrollIndicator = NO;

    _topScrollView.showsVerticalScrollIndicator = NO;

    //_topScrollView.backgroundColor = [UIColor redColor];

    for(NSIntegeri =0; i<_mArrayTitle.count; i++) {

        UIButton*topButton = [[UIButtonalloc]initWithFrame:CGRectMake(i *topButtonWidth,0,topButtonWidth,40)];

        topButton.tag= i-10;

    //    [topButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];

       // [topButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

         [topButtonsetTitle:_mArrayTitle[i] forState:UIControlStateNormal];

         [_mArrayTopButtonaddObject:topButton];

        [_topScrollViewaddSubview:topButton];

        topButton.tag=10+ i;

        [topButtonaddTarget:self action:@selector(clickTopButton:) forControlEvents:UIControlEventTouchUpInside];

        //topButton.selected = NO;



        [topButtonsetTitleColor:[UIColor colorWithRed:122.0/255 green:121.0/255 blue:121.0/255 alpha:1] forState:UIControlStateNormal];


        topButton.selected=NO;

        [topButtonsetTitleColor:[UIColor colorWithRed:178.0/255 green:17.0/255 blue:25.0/255 alpha:1] forState:UIControlStateSelected];

    }


     ((UIButton*)_mArrayTopButton[1]).selected=YES;

    [_topScrollView setContentSize:CGSizeMake(_mArrayTitle.count * topButtonWidth, 40)];

    ((UIButton*)_mArrayTopButton[1]).selected=YES;

     [_topScrollView setContentSize:CGSizeMake(_mArrayTitle.count * topButtonWidth, 40)];


    if(_topScrollView.contentSize.width - _topScrollView.frame.size.width <0)

    {

        [_topScrollView setContentOffset:CGPointMake(-(_topScrollView.frame.size.width - _topScrollView.contentSize.width)/2, 0) animated:NO];

    }


    [self.view addSubview:_topScrollView];


}

//clickTopButton:

-(void)clickTopButton:(UIButton*)sender{

    // 设置Button的选中情况

    for(UIButton*buttonin_mArrayTopButton) {

        if(button.selected==YES) {

            button.selected=NO;


            button.layer.borderColor= [UIColorcolorWithRed:178.0/255green:17.0/255blue:25.0/255alpha:1].CGColor;

            button.layer.borderWidth=1.0f;

            button.layer.masksToBounds=YES;

            button.layer.cornerRadius=15.0f;

            button.backgroundColor= [UIColorcolorWithRed:255.0/255green:224.0/255blue:226.0/255alpha:1];

        }



    }

//    button.backgroundColor = [UIColor clearColor];

//    [button setSelected:NO];

//    button.layer.borderWidth = 0.0f;

//    sender.selected = YES;


    NSLog(@"输出sender.tag:%ld",(long)sender.tag);


}

#pragma mark -uiscrollviewdelegate

-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{

    // 设置Button的选中情况

    for(UIButton*buttonin_mArrayTopButton) {

        if(button.selected==YES) {

            button.selected=NO;

        }

    }

    ((UIButton*)[_mArrayTopButtonobjectAtIndex:scrollView.contentOffset.x/WIDTH]).selected=YES;


}

#pragma mark创建tableview

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

    return 1;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return 5;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{


    informationViewCell *viewcell = [informationViewCell cellWithTableView:tableView];


    viewcell.selectionStyle = UITableViewCellSelectionStyleNone;

    viewcell.contentView.backgroundColor = [UIColor colorWithRed:253.0/255 green:252.0/255 blue:250.0/255 alpha:1];

    returnviewcell;

}

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

    return 100;

}

@end

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UIView+SGFrame.h #import @interface UIView (SGFrame) @pro...
    超越时空的思念_8a22阅读 1,834评论 1 1
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,663评论 3 3
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,705评论 2 7
  • AppDelegate.m #import "AppDelegate.h" #import "ViewContro...
    哈喽Mm阅读 347评论 0 0
  • 今天是年三十,此刻我在高铁上,甚是乏累,忽然想起行李箱里有一本《茶花女》便想翻出来瞧瞧,可是我犹豫了,因为我太怕别...
    简语小江湖阅读 384评论 1 1