ios中的轮播图跟gridview的复用

来源:互联网 发布:yum命令搜索 编辑:程序博客网 时间:2024/04/29 08:16

轮播图现在是比较常用的,所以下面就贴一些代码,供新手跟懒人(请不要对号入座偷笑)使用

轮播图跟gridview复用更是多

-(void)requestFinished:(ASIHTTPRequest *)request

{

    [JYHAPIcloseLoading:self.view];

    

    

    if (request.tag ==100) {

        home_indexdataExist =2;

        NSString *responseStr = [request responseString];

        NSData *jsonData = [responseStr dataUsingEncoding:NSUTF8StringEncoding];

        NSDictionary* dic = [NSJSONSerializationJSONObjectWithData:jsonData options:NSJSONReadingAllowFragmentserror:nil];

        NSDictionary *list = [dic objectForKey:@"list"];

        //NSArray *array = [dic objectForKey:@"list"];

       // NSLog(@"xuhaoarray = %@",array);

        for(NSDictionary*dic1in list)

        {

            HomePageItem *item = [[HomePageItemalloc] init];

            item.adId = [dic1 objectForKey:@"adId"];

            item.title = [dic1 objectForKey:@"title"];

            item.image = [dic1 objectForKey:@"image"];

            item.couponId = [dic1 objectForKey:@"couponId"];

            item.type = [dic1 objectForKey:@"type"];

            item.url = [dic1 objectForKey:@"url"];

            item.open = [dic1 objectForKey:@"open"];

            item.cataId = [dic1 objectForKey:@"cataId"];

            [headImageArray addObject:item.image];

            [headTitleArray addObject:item.title];

            [headViewIDArray addObject:item.adId];

            

            [dataArray addObject:item];

        }

        

        headerView = [[UIViewalloc] init];

        if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7)

        {

             headerView.frame =CGRectMake(0,64,kScreenWidth, 160);

        }else

            headerView.frame =CGRectMake(0, 44,kScreenWidth, 160);

        

        headerView.userInteractionEnabled =YES;

        sv = [[UIScrollViewalloc] initWithFrame:CGRectMake(0,0, kScreenWidth,150)];

        sv.contentSize =CGSizeMake(kScreenWidth * [dataArraycount], 150);

        sv.backgroundColor = [UIColorredColor];

        sv.delegate =self;

        sv.pagingEnabled =YES;

        sv.showsHorizontalScrollIndicator =NO;

        sv.showsVerticalScrollIndicator =NO;

        sv.userInteractionEnabled =YES;

        [headerView addSubview:sv];

        

        pc = [[UIPageControlalloc] initWithFrame:CGRectMake(0,140, kScreenWidth,10)];

        pc.numberOfPages = [dataArraycount];

        

        [headerView addSubview:pc];

        

        

        for (int i =0; i < [dataArray count]; i++)

        {

            UIButton *imageBtn = [[UIButtonalloc]initWithFrame:CGRectMake(i*320,0, 320, 160)];

            

            UIImageView *imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(i*320,0, 320, 160)];

            UILabel *adTitleLable = [[UILabelalloc] initWithFrame:CGRectMake(i*320+5,110, 310, 25)];

            adTitleLable.text = [NSStringstringWithFormat:@"%@",[headTitleArrayobjectAtIndex:i]];

            adTitleLable.backgroundColor = [UIColorgrayColor];

            adTitleLable.font = [UIFontsystemFontOfSize:14];

            adTitleLable.textAlignment = UITextAlignmentCenter;

            

            [imageView setImageWithURL:[NSURLURLWithString:[headImageArray objectAtIndex:i]]placeholderImage:[UIImageimageNamed:@"qq.png"]];

           

            imageView.userInteractionEnabled =YES;

            [imageBtn setBackgroundImage:[UIImageimageNamed:@"qq.png"]forState:UIControlStateNormal];

            [imageBtn addTarget:selfaction:@selector(btn:)forControlEvents:UIControlEventTouchUpInside];

            imageBtn.alpha = 0.05;

            imageBtn.tag = [[headViewIDArrayobjectAtIndex:i]integerValue];

         

            [sv addSubview:imageView];

            [sv addSubview:imageBtn];

            [sv addSubview:adTitleLable];

            

        }

        

        // gridView = [[UIGridView alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, kScreenHeight- 120)];

        gridView = [[UIGridViewalloc] init];

        

        if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7)

        {

            gridView.frame =CGRectMake(0, 64,kScreenWidth, kScreenHeight- 110);

        }else

           gridView.frame =CGRectMake(0, 44,kScreenWidth, kScreenHeight- 120);

        

        gridView.backgroundColor = [UIColorclearColor];

        gridView.uiGridViewDelegate =self;

        gridView.showsHorizontalScrollIndicator =NO;

        gridView.showsVerticalScrollIndicator =NO;

        [gridViewsetTableHeaderView:headerView];

        [self.view addSubview:gridView];


        

        NSTimer *timer = [NSTimerscheduledTimerWithTimeInterval:3.0target:selfselector:@selector(tick)userInfo:nilrepeats:YES];

        [timer fire];

       


    }else{

        home_addataExist =2;

    NSString *responseStr = [request responseString];

    NSData *jsonData = [responseStrdataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary* dic = [NSJSONSerializationJSONObjectWithData:jsonData options:NSJSONReadingAllowFragmentserror:nil];

    NSDictionary *list = [dic objectForKey:@"list"];

    

       

        

    for(NSDictionary*dic1in list)

    {

        HomePageItem *item = [[HomePageItemalloc] init];

        item.cataId = [dic1 objectForKey:@"cataId"];

        item.cataName = [dic1 objectForKey:@"cataName"];

        item.cataLogo = [dic1 objectForKey:@"cataLogo"];

        item.cataIndex = [dic1 objectForKey:@"cataIndex"];

        item.P_cataId = [dic1 objectForKey:@"P_cataId"];

       

         [dataArray1 addObject:item];

    }

        

    [gridViewreloadData];

    }

    

}

-(void)btn:(UIButton *)sender

{

  //  NSLog(@"fsfs=%d",sender.tag);

        for (int i =0; i<headViewIDArray.count; i++) {

            HomePageItem *item = [dataArrayobjectAtIndex:i];

            if (sender.tag == item.adId.integerValue)

            {

                 NSLog(@"xuhaotap撒旦法撒旦法撒旦法==%@",item.type);

                if (item.type.integerValue ==0) {

                    return;

                }else if(item.type.integerValue ==1)

                {

                    if (item.open.integerValue ==0) {

                        [[UIApplication sharedApplication]openURL:[NSURLURLWithString:item.url]];

                    }else

                    {

                        NSLog(@"web");

                        WebViewViewController *web = [[WebViewViewControlleralloc] init];

                        web.url = item.url;

                        [self.navigationControllerpushViewController:web animated:NO];

                    }

                   

                }else if (item.type.integerValue ==2)

                {

                    AllInfoViewController *allInfo = [[AllInfoViewControlleralloc] init];

                    allInfo.ID = item.couponId;

                    [self.navigationControllerpushViewController:allInfo animated:NO];

                }else

                {

//                    AllViewController *all = [[AllViewController alloc] init];

//                    all.cataId = item.cataId;

//                    [self.navigationController pushViewController:all animated:NO];

                    

                    AllViewController *all = [[AllViewControlleralloc] init];

                    all.cataId = item.cataId;

                    NSLog(@"all.cataId=%@",all.cataId);

                    

                    [self.navigationControllerpushViewController:all animated:NO];

                }

                

            }

    

        }

    

}

-(void)backBtnClick

{

    AllViewController *all = [[AllViewControlleralloc]init];

    [self.navigationControllerpushViewController:all animated:NO];

}



//自动播放图片的函数

- (void)tick

{

    

    if ([headImageArraycount] >= 2)

    {

        if (tickIndex >= [headImageArraycount])

        {

            tickIndex = 0;

        }

        

        CGFloat pageWidth = sv.frame.size.width;

        [svsetContentOffset:CGPointMake(tickIndex * pageWidth,0) animated:YES];

        

        pc.currentPage =tickIndex;

        

        courseTitleLable.text = [corseTitleArrayobjectAtIndex:tickIndex];

       

        

        tickIndex ++;

    }

    


}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    NSLog(@"dsdfsaend");

    pc.currentPage = scrollView.contentOffset.x/320;

    tickIndex = pc.currentPage;

    courseTitleLable.text = [corseTitleArrayobjectAtIndex:pc.currentPage];

    

}

此代码是在ARC的环境下编写的,其中有许多不足之处,跪求指正,当然了,不是很明白的也可以问我


原创粉丝点击