.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
.h
#import <UIKit/UIKit.h>
@class ImageCellStyle;
@interface ActionCell : UITableViewCell
@property (nonatomic, strong) ImageCellStyle *imageStyle;
- (void)settingData:(ImageCellStyle *)imagecellStyle;
@end
.m
#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
实现
.m
-(NSArray *)imageStyleArray
{
NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:10];
for (NSInteger i=0;i<10;i++) {
ImageCellStyle *imageStyle=[[ImageCellStyle alloc]init];
imageStyle.picture = @"dfdfs";
imageStyle.name = @"dfsdf";
imageStyle.much = @"fdsf";
imageStyle.time = @"fdfsdf";
imageStyle.style = @"fdfsdf";
[arrayM addObject:imageStyle];
}
imageStyleArray = [arrayM copy];
return imageStyleArray;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return self.imageStyleArray.count;
}
-(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];
}
ImageCellStyle *imageCellStyle=self.imageStyleArray[indexPath.row];
[cell settingData:imageCellStyle];
return cell;
}