您的位置:首页 > 产品设计 > UI/UE

UI09_重写Cell

2015-08-10 21:01 525 查看
// 读出plist文件内容
NSString *path = [[NSBundle mainBundle] pathForResource:
@"Student" ofType:@"plist"];
NSMutableDictionary *dic = [NSMutableDictionary
dictionaryWithContentsOfFile:path];
NSLog(@"%@", dic);

//  除去cell之间的横线
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


Cell 的重写

首先写两个继承于UITableViewCell的类 : MyCell SecondMyCell

MyCell.h 文件

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell

#warning 现在要给自定义的cell加上4条属性, 而且需要在外部进行赋值, 所以在.h里写属性的声明, 而且这四个属性, 他们的名不能和系统已有的属性名重复, 包括imageView, taextLabel, detailTextLabel

@property(nonatomic, retain)UIImageView *leftImageView;
@property(nonatomic, retain)UILabel *upLabel;
@property(nonatomic, retain)UILabel *downLabel;
@property(nonatomic, retain)UIImageView *rightImageView;

@end


MyCell.m 文件

#import "MyCell.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
@implementation MyCell

#pragma  mark 重写cell的初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//  完成对属性视图的创建, 但是一般创建之后不给属性视图frame

[self createView];

}
return  self;
}

#pragma mark 属性视图进行创建

- (void)createView{
//  创建左imageVie
self.leftImageView = [[UIImageView alloc]init];
self.leftImageView.backgroundColor = [UIColor greenColor];
//  添加
//  cell上有一个专门用来显示控件的视图, 叫做contentView, 我们把视图就放到contentView上进行显示
[self.contentView addSubview:self.leftImageView];
[_leftImageView release];

//  创建upLabel
self.upLabel = [[UILabel alloc]init];
self.upLabel.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:self.upLabel];
[_upLabel release];

//  创建downLabel
self.downLabel = [[UILabel alloc]init];
self.downLabel. backgroundColor = [UIColor orangeColor];
[self.contentView addSubview:self.downLabel];
[_downLabel release];

//  创建右imageView
self.rightImageView = [[UIImageView alloc]init];
self.rightImageView.backgroundColor = [UIColor purpleColor];
[self.contentView addSubview:self.rightImageView];
[_rightImageView release];

}

//  pragma mark 这个方法是cell显示之前走的最后的一个方法, 一般会在这个方法里设置所有的属性视图的大小和尺寸, 这个方法会用在图片文字的自适应的设置上
- (void)layoutSubviews{
//  重写了父类的layoutSubviews方法, 如果想要这个方法发挥正常功能, 别忘了[super layoutSubviews]
[super layoutSubviews];
//  对所有属性视图的位置和大小进行设置
self.leftImageView.frame = CGRectMake(0, 0, WIDTH / 3, HEIGHT);
self.upLabel.frame = CGRectMake(WIDTH / 3, 0, WIDTH / 3, HEIGHT / 2);
self.downLabel.frame = CGRectMake(WIDTH / 3, HEIGHT / 2, WIDTH / 3, HEIGHT / 2);
self.rightImageView.frame = CGRectMake(WIDTH / 3 * 2, 0, WIDTH / 3, HEIGHT);
}

- (void)dealloc
{
[_leftImageView release];
[_upLabel release];
[_downLabel release];
[_rightImageView release];
[super dealloc];
}

- (void)awakeFromNib {
// Initialization code
}

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

// Configure the view for the selected state
}

@end


SecondMyCell. h 文件

#import <UIKit/UIKit.h>

@interface SecondMyCell : UITableViewCell
@property(nonatomic, retain)UIImageView *leftImageView;
@property(nonatomic, retain)UIImageView *centerImageView;
@property(nonatomic, retain)UIImageView *rightImageView;
@property(nonatomic, retain)UILabel *leftLabel;
@property(nonatomic, retain)UILabel *rightLabel;
@end


SecondMyCell.m 文件

#import "SecondMyCell.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
@implementation SecondMyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style
reuseIdentifier:reuseIdentifier];
if (self) {
[self createView];
}
return  self;

}

- (void)createView{

self.leftImageView = [[UIImageView alloc]init];
self.leftImageView.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.leftImageView];
[_leftImageView release];

self.centerImageView = [[UIImageView alloc]init];
self.centerImageView.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:self.centerImageView];
[_centerImageView release];

self.rightImageView = [[UIImageView alloc]init];
self.rightImageView.backgroundColor = [UIColor orangeColor];
[self.contentView addSubview:self.rightImageView];
[_rightImageView release];

self.leftLabel = [[UILabel alloc]init];
self.leftLabel.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:self.leftLabel];
[_leftImageView release];

self.rightLabel = [[UILabel alloc]init];
self.rightLabel.backgroundColor = [UIColor purpleColor];
[self.contentView addSubview:self.rightLabel];
[_rightLabel release];
}

- (void)dealloc
{
[_leftImageView release];
[_centerImageView release];
[_rightImageView release];
[_leftLabel release];
[_rightLabel release];
[super dealloc];
}

- (void) layoutSubviews{
[super layoutSubviews];  //   注意不要忘记

self.leftImageView.frame = CGRectMake(0, 0, WIDTH / 3, HEIGHT / 2);
self.centerImageView.frame = CGRectMake(WIDTH / 3, 0, WIDTH / 3, HEIGHT / 2);
self.rightImageView.frame = CGRectMake(WIDTH / 3 * 2, 0, WIDTH / 3, HEIGHT/ 2);

self.leftLabel.frame = CGRectMake(0, HEIGHT / 2, WIDTH / 2  , HEIGHT / 2);
self.rightLabel.frame = CGRectMake(WIDTH /  2, HEIGHT / 2, WIDTH / 2 , HEIGHT / 2);

}

- (void)awakeFromNib {
// Initialization code
}

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

// Configure the view for the selected state
}

@end


根据奇偶的不同, 将重写的两个cell显示出来

MainViewController.m 文件

#import "MainViewController.h"
#import "MyCell.h"
#import "SecondMyCell.h"
#import "Student.h"
@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UITableView *tableView;
@end

@implementation MainViewController

- (void)dealloc
{
[_arr release];
[_tableView release];
[super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.arr = [NSMutableArray arrayWithObjects:@"宋江1", @"卢俊义2", @"吴用3", @"公孙胜4", @"关胜5", @"林冲6", @"秦明7" ,@"呼延灼8" , @"花容9",@"柴进10", @"李应11", @"朱仝12",@"鲁智深13",@"武松14",nil];

[self createData];
}
return self;

}

- (void)createData{
NSString *path = [[NSBundle mainBundle]pathForResource:@"StudentArr" ofType:@"plist"];
NSArray *stuArr = [NSArray arrayWithContentsOfFile:path];
NSDictionary *dic = stuArr[0];
//NSLog(@"%@", dic[@"name"]);
//  通过KVC对model进行赋值
Student *stu = [[Student alloc]init];
[stu setValuesForKeysWithDictionary:dic];
NSLog(@"%@", stu.name);
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.navigationController.navigationBar.translucent = NO;
self.view.backgroundColor = [UIColor orangeColor];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
[_tableView release];

self.tableView.dataSource = self;
self.tableView.delegate = self;

self.tableView.rowHeight = 150;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return  self.arr.count;
}

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

if (indexPath.row % 2 == 1) {
static NSString *reuse = @"reuse";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[MyCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
}
cell.upLabel.text = self.arr[indexPath.row];
cell.leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", indexPath.row + 1]];
cell.rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", indexPath.row + 1]];
return  cell;
}else{
static NSString *reuse = @"secondReuse";
SecondMyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[SecondMyCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
}
cell.leftLabel.text = self.arr[indexPath.row];
return  cell;
}

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: