ios中重写CollectionViewCell

来源:互联网 发布:manifest.json 编辑:程序博客网 时间:2024/05/22 18:22

RZMars个人Blog地址: http://www.rzmars.com

在IOS6中添加了许多新的API,UICollectionView 也就是UITableView的亲弟弟可能是最热的了今天我们就来说一说在UICollectionView中最为重要的一个对象UICollectionViewCell的重写来侃侃:更多内容请登陆 www.rzmars.com
UICollectionViewCell 继承于UICollectionReusableView(继承UIView)
在UICollectionViewCell中最要的就是@property (nonatomic, readonly) UIView *contentView; // add custom subviews to the cell’s contentView这个属性了 因为我们平常到是self.view 来添加子控件 这次要用contentView来添加了:
重写的方式大同小异就不多说了 直接贴上代码:
MyCollectionView.h文件:

#import <UIKit/UIKit.h>

@interface MyCollectionView : UICollectionViewCell
-(void)setImage:(UIImage*)image;
-(void)setImgTitle:(NSString *)tiitle;
-(void)setImageTitleLabelWidth:(CGFloat)width withHight :( CGFloat)hight;
-(void)setimageTitleTextColor:(UIColor *)bgColor withTextColor:(UIColor *)textColor;

@end

MyCollectionView.m 文件:

//
// MyCollectionView.m
// CollectionViewCellDemo
//
// Created by RZMars on 10/17/13.
// Copyright (c) 2013 RZMars. All rights reserved.
//

#import “MyCollectionView.h”

 

@interface MyCollectionView ()

@property(strong,nonatomic) UIImageView * _mImage;
@property(strong,nonatomic) UITextView * _mTextView;

 

@end

@implementation MyCollectionView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self._mImage = [[UIImageView alloc] initWithFrame:CGRectMake(0., 0., frame.size.width, frame.size.height)];
}
return self;
}

-(void)setImage:(UIImage*)image{
self._mImage.image=image;

}
-(void)setImgTitle:(NSString *)tiitle{

if([self.contentView subviews]){
for ( UILabel *subView in [self.contentView subviews] ) {
[subView removeFromSuperview];
}
}
[self.contentView addSubview:self._mImage];
[self._mTextView setText:tiitle];
[self.contentView addSubview:self._mTextView];

}
-(void)setImageTitleLabelWidth:(CGFloat)width withHight :( CGFloat)hight{
self._mTextView =[[UITextView alloc] initWithFrame:CGRectMake(20, self._mImage.frame.size.height/2+18, width, hight)];
self._mTextView.contentInset=UIEdgeInsetsMake(1, 1, 1, 1);
self._mTextView.userInteractionEnabled=NO;
}
-(void)setimageTitleTextColor:(UIColor *)bgColor withTextColor:(UIColor *)textColor{
self._mTextView.textColor = textColor;
self._mTextView.backgroundColor= bgColor;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

@end
使用:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

MyCollectionView * imgView =[[MyCollectionView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];

[imgView setImage:[UIImage imageNamed:@"logo"]];

[imgView setImageTitleLabelWidth:60 withHight:30];
[imgView setimageTitleTextColor:[UIColor redColor] withTextColor:[UIColor greenColor]];
[imgView setImgTitle:@"RZMars"];
[self.view addSubview:imgView];
}

好了 是不是很简单? 下节我们将学习UITableView的亲弟弟的使用!!!!!

バイバイ


原创粉丝点击