.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