zoukankan      html  css  js  c++  java
  • iOS使用UIScrollView实现左右滑动UITableView和UICollectionView

    在UIScrollView嵌套UITableView这篇文章是非常,但该项目的需求,需要嵌套UICollectionView,和UICollectionView和UITableView有非常多的不同,有些知识到现在也没搞清楚,一遍又一遍的尝试,最后做出来的。

    图:


    因为本人刚刚接触ios开发,非常多原理还说不清,所以以下的步骤以图片为主。文章结尾会附上源代码地址。可下载自行研究!

    1、新建项目


    2、改动storyboard,因为要使用到导航栏,所以删除原有view,从工具箱中拖入NavigationController。并将入口(剪头)指向该view。删除自带的tableviewcontroller,拖入view controller。例如以下图


    3、新建tableviewcontroller,tableviewcontroller默认带有tableview的视图,所以不须要勾选“also create xib file”;可是collection viewcontroller就不行。这点比較郁闷。


    4、UICollectionViewController不能直接使用,測试了非常久。就是不能嵌套在scrollview中。所以仅仅能先创建view controller,再包括collection view,须要创建xib文件;打开xib文件拖入Collection View,并将此视图关联至

    @property (weaknonatomicIBOutletUICollectionView *collection;


    5、collectionviewcontroller就比較麻烦了。首先创建CollectionView所使用的单元格CollectionViewCell;并新建一个空的xib;



    6、打开CollectionCell.xib,从工具箱拖入Collection Cell。设置背景色为黄色,并拖入一个label控件;注意设置Collection Cell的class 为刚才建立的“CollectionCell”类(不是Files Owner);关联

    IBOutletUILabel *label

    。例如以下图所看到的


    至此。全部页面及前台已经设置完成

    8、先搞定tableviewcontroller,例如以下代码

    //
    //  TMJTableViewController.m
    //  PageTest
    //
    //  Created by ejiang on 14-6-30.
    //  Copyright (c) 2014年 daijier. All rights reserved.
    //
     
    #import "TMJTableViewController.h"
     
    @interfaceTMJTableViewController ()
    
    
     
    @end
     
    
    @implementation TMJTableViewController
    
     
    
    - (id)initWithStyle:(UITableViewStyle)style
    
    {
    
    self = [super initWithStyle:style];
    
    if (self) {
            // Custom initialization
        }
        returnself;
    
    
    }
    
     
    
    - (void)viewDidLoad
    
    {
        [superviewDidLoad];
    
    
    }
    
     
    
    - (void)didReceiveMemoryWarning
    
    {
        [superdidReceiveMemoryWarning];
    
    
    }
    
     
    #pragma mark - Table view data source
     
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    
    {
    
    return 1;
    
    }
    
     
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    
    {
    
    return 10;
    
    }
    
     
    
     
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
    {
    
    static NSString *cellIdentifier=@"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    
        if(cell==nil)
    
    {
            cell=[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
    
    
        }
        cell.textLabel.text=@"哈哈";
    
    
        return cell;
    
    }
    @end

    9、还是看源代码吧,粘贴代码没意思,主要步骤就以上几部

    源代码下载地址:http://download.csdn.net/detail/wuwo333/8098431


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    Java面试题集(七)--Spring常见面试问题【重要】
    Java面试题集(六)
    qt4.8.4安装以及64位程序编译方法
    页面跳转动画设置方法
    Lua环境配置 windows + VS
    Oracle触发器(trigger):view,schema,database
    java课程设计(计算器)
    数据结构 练习 19-活动选择问题的实现(动态规划 和 贪心)
    网页在Safari快速滚动和回弹的原理: -webkit-overflow-scrolling : touch;的实现
    如何打开Nib文件
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4732295.html
Copyright © 2011-2022 走看看