UIKite/UICollectionViewController/ScrollView

</pre> UIButton -->UIControl-->UIVIew<p></p><p> UILabel--->UIView</p><p>UIImageView-->UIView</p><p>UITextField -->UIView</p><p>UIControl可以交互,它具有以下属性和方法:</p><p></p><pre code_snippet_id="635768" snippet_file_name="blog_20150404_2_9506459" name="code" class="objc"> 1.1 设置控件的状态 
启用、禁用
 @property(nonatomic,getter=isEnabled) BOOL enabled;
 选中、不选中
 @property(nonatomic,getter=isSelected) BOOL selected;
 高亮或者不高亮
 @property(nonatomic,getter=isHighlighted) BOOL highlighted;

 1.2 设置控件内容的布局
 垂直居中方向
 @property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
 水平居中方向
 @property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;

 1.3 添加/删除监听方法
 - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
 - (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

//将监听方法,注册到“”运行循环“”,当按钮被点击以后,运行循环通知视图控制器执行@selector的方法

btn addTarget :self action @selector

代理的用处?

 监听那些不能通过addTarget监听的事件!

主要用来负责在两个对象之间,发生某些事件时,来传递消息或者数据

 2> 代理的实现步骤
 (1)    成为(子)控件的代理,父亲(控制器)成为儿子(文本框)的代理
 (2)    遵守协议->利用智能提示,快速编写代码
 (3)    实现协议方法

UIScrollView相关:

   // 告诉scrollView内部内容的实际大小
   self.scrollView.contentSize = image.size;

如果 UIScrollView 无法滚动,可能是以下原因:
没有设置contentSize
scrollEnabled=NO
没有接收到触摸事件:userInteractionEnabled=NO
没有取消autolayout功能(如果在Storyboard中添加了ScrollView的子控件,要想scrollView滚动,必须取消autolayout)
content Inset:设置边距(相当于padding)

 self.scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
    // 不显示水平滚动标示
    self.scrollView.showsHorizontalScrollIndicator = NO;
    // 不显示垂直滚动标示
    self.scrollView.showsVerticalScrollIndicator = NO;

    // *** 偏移位置
    self.scrollView.contentOffset = CGPointMake(0, -100)

    // 取消弹簧效果,内容固定,不希望出现弹簧效果时
    // 不要跟bounds属性搞混了
    self.scrollView.bounces = NO;


    // 让图像视图根据图像自动调整大小,而不用frame把宽高写死
    [self.imageView sizeToFit];

自动布局:
苹果为了程序员能够将注意力集中在程序上,而不要在代码中过多的使用frame
“参照”任何一个控件,都可以参照另外一个控件定义出准确的位置

xcode复制控件:按着option,然后鼠标拖动。然后先后松开署标和option键。

strong 与 weak的区别:

/** ARC中dealloc主要用于调试,判断对象是否被释放 */
- (void)dealloc
{
    NSLog(@"Person对象被释放");
}

 在OC中如果对象没有强引用,会被立即释, 默认的对象,都是强引用的
 __weak 表示弱引用,如: __weak Person *p = [[Person alloc] init] 表示对象赋值以后就会被释放

当向数组里面存的时候,会自动强引用;
一般只有一个对象进行强引用即可,否则内存可能泄漏
对象的属性声明方式:
如果用stroyboard拖线,控件用weak,如果用代码创建界面,控件用strong,自定义对象需要使用strong,NSString需要使用copy,数字型(如int),使用assing 

加载xib时要注意,返回的究竟是不是View对象(一般xib放一个根view对象,从而可以保证能正确返回)。

可以用xib添加手势识别器(UITapGestureRecognizer),然后在xib里面向代码里面连线添加监听方法

使用xib加载控件时抛出如下异常:
// -[UITapGestureRecognizer superview]: unrecognized selector sent to instance 0x8e407a0   
// 分析:
<pre name="code" class="objc">superview方法属于谁:UIView
// 结论:
// 错误将UITapGestureRecognizer当做UIView来使用(从xib中加载的不是UIView而是UITapGestureRecognizer)
解决方法:
    NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"HMToolbar" owner:nil options:nil];
    [objs lastObject];//必须知道返回的是view对象

 xib错误二(注意xib名称): load the "xxx" nib but the view outlet was not set问题分析: 

     HMHomeViewController *homeVc = [[HMHomeViewController alloc] init]; //默认会尝试去找HMHomeView.xib,如果没有会尝试去找与控制器同名(HMHomeViewController.xib)的xib,如果找不到,则自己创建
    homeVc.view.backgroundColor = [UIColor redColor];
    homeVc.title = @"首页界面";
    [self.navigationController pushViewController:homeVc animated:YES];  
  // 控制器默认会自动找对应的xib来创建view
    // 1.去掉Controller这个单词后的同名的xib:HMHomeView.xib
    // 2.完全同名的xib:HMHomeViewController.xib
 解决方法:
  1,在xib中设置files owner
    2,xib名字不要与控制器前缀相同

一般修改了xib存在缓存:在模拟器将项目删除掉,然后清理工程(product-->clear)

自定义表格行:

XIB的定义步骤
1> 新建HMTgCell.xib(com+N-->empty) //单元格类名相同
2> 拽一个需要自定义的控件,摆放其他子控件
3>	新建一个类
*	<span style="color:#FF0000;">类名要与XIB的名字保持一致</span>
*	<span style="color:#FF0000;">继承自的子类要与XIB中的根节点的类型一致</span>
4>	<span style="color:#FF0000;"><strong>要连线之前,需要将XIB的根节点类名(custom class)修改为刚刚新建的类名,否则不能自动关联(Automatic)</strong></span>
5>	连线
6>	在XIB的属性面板,指定可重用标示符(identifiter),否则表格行不能重用 

利用xib加载cell:
<pre name="code" class="objc">// 1. 可重用标示符
    static NSString *ID = @"Cell"; //这里的Cell要与XIB的属性面板中指定重用标示符相同,否则不能重用
    // 2. tableView查询可重用Cell
    HMTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 3. 如果没有可重用cell
    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        // 从XIB加载自定义视图
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HMTgCell" owner:nil options:nil] lastObject];
    }
    
代码创建Cell的步骤
-----------------------------------------------
1>    创建自定义Cell,继承自UITableViewCell
2>    根据需求,确定控件,并定义属性
3>    用getter方法完成控件的实例化,只创建并添加到contentView,不处理位置
4> 定义一个模型属性,通过setter方法,设置cell的显示

 
UICollectionViewController的使用: 

1,新建一个类继承自:UICollectionViewController

2,在.m文件实现代理方法:

// 返回有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//共有多少个cell
 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.products.count;
}

// 返回每一个cell长什么样
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{     <pre name="code" class="objc">    UICollectionViewCell *cell = [collectionView <span style="color:#FF0000;">dequeue</span>ReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];    
    ....
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ILProduct *p = self.products[indexPath.item];
    NSLog(@"点击了---%@",p.title);
}
3,注册UICollectionViewCell:
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 注册UICollectionViewCell,如果没有从缓存池找到,就会自动帮我们创建UICollectionViewCell
    UINib *xib = [UINib nibWithNibName:@"ILProductCell" bundle:nil];
    [self.collectionView registerNib:xib forCellWithReuseIdentifier:ID];
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

4,<span style="color:#FF0000;">实例化UICollectionView需要指定一个布局</span>(流水布局):
方法一直接实例化: UIXXXXCollectionView  alloc] initWithColllectionViewLayout:<span style="color:#FF0000;">UICollectionViewFlowLayout</span>]
方法二重写控制器的init方法:
- (id)init
{
    // 创建流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 每一个cell的尺寸
    layout.itemSize = CGSizeMake(80, 80);  
    // 垂直间距
    layout.minimumLineSpacing = 10; 
    // 水平间距
    layout.minimumInteritemSpacing = 0; 
    // 内边距(padding)
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);
    return [super initWithCollectionViewLayout:layout];
}

关键点:
<span style="color:#FF0000;"> 实现代理方法;必须有布局;cell必须自己注册</span>

 ios快速解析JSON: 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值