自定義Cell幾種方法

.h文件

#import <UIKit/UIKit.h>

@class ImageCellStyle;

@interface ActionCell : UITableViewCell

@property (nonatomic, strong) ImageCellStyle *imageStyle;

- (void)settingData:(ImageCellStyle *)imagecellStyle;

@end

.m

//
//  ActionCell.m
//  影樓
//
//  Created by Jane on 4/21/15.
//  Copyright (c) 2015 Jane. All rights reserved.
//

#import "ActionCell.h"
#import "ImageCellStyle.h"

#define NJNameFont [UIFont systemFontOfSize:13]
#define NJTextFont [UIFont systemFontOfSize:14]

@interface ActionCell ()

@property (nonatomic, retain) UIImageView *pictureView;

@property (nonatomic, retain) UILabel *nameView;
@property (nonatomic, retain) UILabel *timeView;

@property (nonatomic, retain) UILabel *muchView;

@property (nonatomic, retain) UILabel *styleView;

@end

@implementation ActionCell
@synthesize pictureView,nameView,muchView,styleView,timeView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        pictureView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 100, 100)];
        [self.contentView addSubview:pictureView];


        nameView = [[UILabel alloc] initWithFrame:CGRectMake( 110, 10, 50, 20)];
        [self.contentView addSubview:nameView];
        nameView.font = NJNameFont;
        //nameView.backgroundColor = [UIColor blueColor];

        [self.contentView addSubview:self.nameView];


        timeView = [[UILabel alloc]initWithFrame: CGRectMake( 110, 30, 50, 20)];
        [self.contentView addSubview:timeView];



        muchView = [[UILabel alloc]initWithFrame: CGRectMake( 110, 50, 50, 20)];
        muchView.font = NJNameFont;
        [self.contentView addSubview:muchView];


        styleView = [[UILabel alloc] initWithFrame: CGRectMake( 110, 70, 50, 20)];
        styleView.font = NJNameFont;
        [self.contentView addSubview:styleView];

    }
    return self;
}

- (void)setStatusFrames:(ImageCellStyle *)statusModle
{
    _imageStyle = statusModle;
}

#pragma mark 設置數據
- (void)settingData:(ImageCellStyle *)imagecellStyle
{

    self.pictureView.image = [UIImage imageNamed:@"1.png"];

    self.nameView.text = imagecellStyle.name;


    self.muchView.text = imagecellStyle.much;

    self.timeView.text = imagecellStyle.style;


    self.styleView.text = imagecellStyle.time;

}

/**
 *  根據文本獲取文本佔用的大小
 *
 *  @param string  文本
 *  @param font    字體
 *  @param maxSize 最大的寬高
 *
 *  @return  =
 */
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *dict = @{NSFontAttributeName:font};
    //    Size:文本能佔用的最大寬高
    //    options: ios提供的計算方式
    //    attributes: 字體和大小
    //    context: nil
    // 如果計算的文本超過了給定的最大的寬高,就返回最大寬高,如果沒有超過,就返回真實佔用的寬高
    CGRect rect =  [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return rect.size;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

.h

#import <Foundation/Foundation.h>

@interface ImageCellStyle : NSObject

@property (nonatomic,copy) NSString *style;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *time;
@property (nonatomic,copy) NSString *much;
@property (nonatomic,copy) NSString *picture;

- (instancetype) initWithDict: (NSDictionary *)dict;//對象方法

+ (instancetype) weiboWithDict: (NSDictionary *)dict;//類方法

@end

.m

#import "ImageCellStyle.h"

@implementation ImageCellStyle


-(instancetype)initWithDict:(NSDictionary *)dict
 {
    if (self = [super init]) {
         //使用KVC
                 [self setValuesForKeysWithDictionary:dict];
        }
    return self;
}


+(instancetype)weiboModelWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}

@end

controller 方法
.m

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0f;
}

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


    static NSString *cellIndentifiter = @"CellIndentifiter";

    ActionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter];

    if (cell == nil) {
        cell = [[ActionCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifiter];
    }

    [cell settingData:_image];
    return cell;
}

第二種通過xib創建
1、新建類,基於UITableViewCell
例如 @interface CellSearch : UITableViewCell

2、新建空 xib 文件
例如命名爲 CellSearch.xib (名字要跟類名一樣)
拖一個UITableViewCell控件到xib中
(你可以在這個cell裏面放你想放的東東)

3、使用
經過1、2步,自定義的cell已經建好了
在cellForRowAtIndexPath 方法中使用

CellSearch *cell = [tableView dequeueReusableCellWithIdentifier:@”CellSearch”];
if(!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@”CellSearch” owner:self options:nil]lastObject];
}
return cell;

運行看效果

4、關聯xib與類文件(這個關聯我經常弄錯)
(到第3步,你已經可以看到自定義的cell了,但是還不能獲取cell裏面的信息,因爲沒有關聯)
選中CellSearch.xib中的UITableViewCell控件,將類名關聯爲CellSearch
(然後就是Cell內普通的控件關聯了)

5、使用
在cellForRowAtIndexPath 方法中使用
例如:
CellSearch *cell = [tableView dequeueReusableCellWithIdentifier:@”CellSearch”];
if(!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@”CellSearch” owner:self options:nil]lastObject];
}
cell.nameTextField.text = @”tom”; //關聯後可以直接設置cell內的控件
return cell;
第三種就是直接在


#import <Foundation/Foundation.h>
#import "Global.h"

@interface YYtg : NSObject
@property(nonatomic,copy)NSString *buyCount;
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *price;
@property(nonatomic,copy)NSString *title;
YYinitH(tg)
@end

#import "YYtg.h"

@implementation YYtg
YYinitM(tg)
@end
#import "YYViewController.h"
#import "YYtg.h"

@interface YYViewController ()<UITableViewDataSource>
@property(nonatomic,strong)NSArray *tg;
@property (strong, nonatomic) IBOutlet UITableView *tableview;

@end

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableview.rowHeight=100;

}

#pragma mark-  懶加載
-(NSArray *)tg
{
    if (_tg==nil) {
        NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];
        NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];

        NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count];
        for (NSDictionary *dict in temparray) {
            YYtg *tg=[YYtg tgWithDict:dict];
            [arrayM addObject:tg];
        }
        _tg=[arrayM mutableCopy];
    }
    return _tg;
}

#pragma mark-數據顯示
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tg.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //讀取xib中的數據
//    NSArray *arrayM=[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil];
//    UITableViewCell *cell=[arrayM firstObject];
    static NSString *identifier=@"tg";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
       // cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell= [[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil] firstObject];
    }

    YYtg *tg=self.tg[indexPath.row];
    //設置數據
    //使用tag
    UIImageView *imgv=(UIImageView *)[cell viewWithTag:1];
    imgv.image=[UIImage imageNamed:tg.icon];
    UILabel *buyCount=(UILabel *)[cell viewWithTag:4];
    buyCount.text=[NSString stringWithFormat:@"已有%@人購買",tg.buyCount];
    UILabel *title=(UILabel *)[cell viewWithTag:2];
    title.text=tg.title;
    UILabel *price=(UILabel *)[cell viewWithTag:3];
    price.text=[NSString stringWithFormat:@"$%@",tg.price];


    //返回cell
    return cell;
}

//隱藏狀態欄
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章