UI09_自定义cell

来源:互联网 发布:rf mems 知乎 编辑:程序博客网 时间:2024/05/21 10:28
////  AppDelegate.m//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 Clare. All rights reserved.//#import "AppDelegate.h"#import "MainViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (void)dealloc{    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    [_window release];        MainViewController *mainVC = [[MainViewController alloc] init];    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:mainVC];    self.window.rootViewController = naVC;    [mainVC release];    [naVC release];    return YES;}
////  MainViewController.m//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 Clare. All rights reserved.//#import "MainViewController.h"#import "MyCell.h"#import "NewCellTableViewCell.h"#import "Student.h"@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>@property(nonatomic, retain)UITableView *tableview;@property(nonatomic, retain)NSArray *arr;@end@implementation MainViewController- (void)dealloc{    [_tableview release];    [_arr release];    [super dealloc];}- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];        [self createData];    }    return self;}- (void)createData{    NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];    NSArray *stuArr = [NSArray arrayWithContentsOfFile:path];    NSDictionary *dic = stuArr[0];    // 通过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.view.backgroundColor = [UIColor whiteColor];    self.navigationController.navigationBar.translucent = NO;    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];    [self.view addSubview:self.tableview];    self.tableview.delegate = self;    self.tableview.dataSource = self;    [_tableview release];    self.tableview.rowHeight = 100;    }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 10;}- (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:@"21.png"];        cell.rightImageView.image = [UIImage imageNamed:@"26.png"];        return cell;    } else    {        static NSString *reuse = @"newReuse";        NewCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];        if (!cell) {            cell = [[[NewCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];        }        cell.leftImage.image = [UIImage imageNamed:@"21.png"];        cell.rightImage.image = [UIImage imageNamed:@"22.png"];        cell.midImage.image = [UIImage imageNamed:@"26.png"];        cell.leftLabel.text = self.arr[indexPath.row];        return cell;    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end

////  MyCell.h//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 Clare. All rights reserved.//#import <UIKit/UIKit.h>@interface MyCell : UITableViewCell#warning 现在要给自定义的cell加上4条属性,而且需要在外部进行赋值,所以在.h写属性的声明,而且这四个属性,他们的名称不能和系统的已有的属性名重复,包括imageView,textLabel,detailTextLabel@property(nonatomic, retain)UIImageView *leftImageView;@property(nonatomic, retain)UILabel *upLabel;@property(nonatomic, retain)UILabel *downLabel;@property(nonatomic, retain)UIImageView *rightImageView;@end

////  MyCell.m//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 Clare. All rights reserved.//#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{    // 创建左imageview    self.leftImageView = [[UIImageView alloc] init];    self.leftImageView.backgroundColor = [UIColor grayColor];        // 添加    // cell上有一个专门用来显示控件的视图,叫contentView,我们把视图就放到contentView上进行显示    [self.contentView addSubview:self.leftImageView];    [_leftImageView release];            // 上label    self.upLabel = [[UILabel alloc] init];    self.upLabel.backgroundColor = [UIColor yellowColor];    [self.contentView addSubview:self.upLabel];    [_upLabel release];        // 下label    self.downLabel = [[UILabel alloc] init];    self.downLabel.backgroundColor = [UIColor cyanColor];    [self.contentView addSubview:self.downLabel];    [_downLabel release];        // 右imageview    self.rightImageView = [[UIImageView alloc] init];    self.rightImageView.backgroundColor = [UIColor greenColor];    [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

////  NewCellTableViewCell.h//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 Clare. All rights reserved.//#import <UIKit/UIKit.h>@interface NewCellTableViewCell : UITableViewCell@property(nonatomic, retain)UIImageView *leftImage;@property(nonatomic, retain)UIImageView *midImage;@property(nonatomic, retain)UIImageView *rightImage;@property(nonatomic, retain)UILabel *leftLabel;@property(nonatomic, retain)UILabel *rightLabel;@end

////  NewCellTableViewCell.m//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 Clare. All rights reserved.//#import "NewCellTableViewCell.h"#define WIDTH self.contentView.frame.size.width#define HEIGHT self.contentView.frame.size.height@implementation NewCellTableViewCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        [self createView];    }    return self;}- (void)createView{    self.leftImage = [[UIImageView alloc] init];    self.leftImage.backgroundColor = [UIColor redColor];    [self.contentView addSubview:self.leftImage];    [_leftImage release];        self.midImage = [[UIImageView alloc] init];    self.midImage.backgroundColor = [UIColor blackColor];    [self.contentView addSubview:self.midImage];    [_midImage release];        self.rightImage = [[UIImageView alloc] init];    self.rightImage.backgroundColor = [UIColor yellowColor];    [self.contentView addSubview:self.rightImage];    [_rightImage release];        self.leftLabel = [[UILabel alloc] init];    self.leftLabel.backgroundColor = [UIColor greenColor];    [self.contentView addSubview:self.leftLabel];    [_leftLabel release];        self.rightLabel = [[UILabel alloc] init];    self.rightLabel.backgroundColor = [UIColor blueColor];    [self.contentView addSubview:self.rightLabel];    [_rightLabel release];}- (void)layoutSubviews{    [super layoutSubviews];        self.leftImage.frame = CGRectMake(0, 0, WIDTH / 3, HEIGHT / 2);    self.midImage.frame = CGRectMake(WIDTH / 3, 0, WIDTH / 3, HEIGHT / 2);    self.rightImage.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)dealloc{    [_leftImage release];    [_midImage release];    [_rightImage release];    [_leftLabel release];    [_rightLabel 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

////  Student.h//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 Clare. All rights reserved.//#import <Foundation/Foundation.h>@interface Student : NSObject// 四条和字典对应的属性,属性名和字典中的key名保持一致@property(nonatomic, copy)NSString *name;@property(nonatomic, copy)NSString *sex;@property(nonatomic, copy)NSString *hobby;@property(nonatomic, copy)NSString *phone;@end

////  Student.m//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 Clare. All rights reserved.//#import "Student.h"@implementation Student- (void)dealloc{    [_name release];    [_sex release];    [_hobby release];    [_phone release];    [super dealloc];}// 只要没找到相匹配的key就走这个方法/// 一定要写上// 如果使用KVC,这个方法一定要写上,避免了因为key和属性名不匹配造成的不必要的崩溃- (void)setValue:(id)value forUndefinedKey:(NSString *)key{    NSLog(@"%@", key);    if ([key isEqualToString:@"sex"]) {            }}@end


0 0