UI09_自定义cell

来源:互联网 发布:中控考勤机数据库修改 编辑:程序博客网 时间:2024/05/21 07:20
////  AppDelegate.h//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end
////  AppDelegate.m//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 flg. 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;}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

////  MainViewController.h//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface MainViewController : UIViewController@end

////  MainViewController.m//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 flg. All rights reserved.//#import "MainViewController.h"#import "MyCell.h"#import "MySecondCell.h"#import "Student.h"@interface MainViewController ()<UITabBarControllerDelegate,UITableViewDataSource>@property(nonatomic,retain)UITableView *tabelView;@property(nonatomic,retain)NSMutableArray *arr;@end@implementation MainViewController- (void)dealloc{    [_tabelView 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:@"StudentArr" ofType:@"plist"];    NSArray *stuArr=[NSArray arrayWithContentsOfFile:path];    NSDictionary *dic=stuArr[0];    //通过kvc对model进行赋值    Student *stu=[[Student alloc] init];    [stu setValuesForKeysWithDictionary:dic];    NSLog(@"%@",stu.name);    NSLog(@"%@",stu.sex1);}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationController.navigationBar.translucent=NO;    self.tabelView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];    [self.view addSubview:self.tabelView];    [_tabelView release];    //self.tabelView.delegate=self;    self.tabelView.dataSource=self;    //self.tabelView.rowHeight=100;}-(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:@"0.jpg"]; cell.rightImageView.image=[UIImage imageNamed:@"1.jpg"];        self.tabelView.rowHeight=100;        return cell;    }else{        static NSString *reuse2=@"reuse2";    MySecondCell *cell2=[tableView dequeueReusableCellWithIdentifier:reuse2];    if (!cell2) {        cell2=[[[MySecondCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse2] autorelease];    }    cell2.firstImage.image=[UIImage imageNamed:@"3.jpg"];    cell2.secondImage.image=[UIImage imageNamed:@"4.jpg"];    cell2.thirdImage.image=[UIImage imageNamed:@"2.jpg"];        self.tabelView.rowHeight=200;    cell2.leftLabel.text=self.arr[indexPath.row];    cell2.rightLabel.text=self.arr[indexPath.row];    return cell2;    }}- (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

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

////  MySecondCell.m//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 flg. All rights reserved.//#import "MySecondCell.h"#define WIDTH self.contentView.frame.size.width#define HEIGHT self.contentView.frame.size.height@implementation MySecondCell- (void)dealloc{    [_firstImage release];    [_secondImage release];    [_thirdImage release];    [_leftLabel release];    [_rightLabel release];    [super dealloc];}-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        [self create];    }    return self;}-(void)create{    self.firstImage=[[UIImageView alloc] init];    [self.contentView addSubview: self.firstImage];    [_firstImage release];    self.secondImage=[[UIImageView alloc] init];    [self.contentView addSubview:self.secondImage];    [_secondImage release];    self.thirdImage=[[UIImageView alloc] init];    [self.contentView addSubview:self.thirdImage];    [_thirdImage release];    self.leftLabel=[[UILabel alloc] init];    [self.contentView addSubview:self.leftLabel];    self.leftLabel.backgroundColor=[UIColor orangeColor];    [_leftLabel release];    self.rightLabel=[[UILabel alloc] init];    [self.contentView addSubview:self.rightLabel];    self.rightLabel.backgroundColor=[UIColor lightGrayColor];    [_rightLabel release];}- (void)layoutSubviews{    [super layoutSubviews];    self.firstImage.frame=CGRectMake(0, 0, WIDTH/3, HEIGHT/2);    self.secondImage.frame=CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT/2);    self.thirdImage.frame=CGRectMake(2*WIDTH/3, 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

////  MyCell.h//  UI09_自定义cell////  Created by dllo on 15/8/10.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface MyCell : UITableViewCell#warning 现在要给自定义的cell添加4条属性,要在外面进行赋值,在.h中写属性.属性名不能和系统的属性名重名..包括imageview,textlabel,detailtextlable@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年 flg. 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;}-(void)dealloc{    [_downLabel release];    [_upLabel release];    [_rightImageView release];    [_leftImageView release];    [super dealloc];}-(void)createView{#pragma mark 属性视图进行创建    //创建左view    self.leftImageView=[[UIImageView alloc] init];    self.leftImageView.backgroundColor=[UIColor orangeColor];    //添加    //cell上有一个专门用来现实空间的视图叫contentView,把视图放在contentView上进行显示    [self.contentView addSubview:self.leftImageView];    [_leftImageView release];    //创建有view    self.rightImageView=[[UIImageView alloc] init];    self.rightImageView.backgroundColor=[UIColor orangeColor];    [self.contentView addSubview:self.rightImageView];    [_rightImageView release];    //创建上label    self.upLabel=[[UILabel alloc] init];    self.upLabel.backgroundColor=[UIColor lightGrayColor];    [self.contentView addSubview:self.upLabel];    [_upLabel release];    //创建下label    self.downLabel=[[UILabel alloc] init];    self.downLabel.backgroundColor=[UIColor yellowColor];    [self.contentView addSubview:self.downLabel];    [_downLabel release];}#pragma mark 这个方法是cell显示之前走的最后一个方法,一般会在这个方法里设置所有的属性视图的大小和尺寸,这个方法会用在图片文字自适应的设置上- (void)layoutSubviews{//重写了父类的layoutSubviews的方法,如果想要这个方法发挥正常的功能,别忘了[super layoutSubviews]    [super layoutSubviews];    //对所有的属性视图的位置大小设置..    self.leftImageView.frame=CGRectMake(0, 0, WIDTH/3, HEIGHT);    self.rightImageView.frame=CGRectMake(2*WIDTH/3, 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);}- (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年 flg. All rights reserved.//#import <Foundation/Foundation.h>@interface Student : NSObject//四条和字典对应的属性@property(nonatomic,copy)NSString *name;@property(nonatomic,copy)NSString *phone;@property(nonatomic,copy)NSString *sex1;@property(nonatomic,copy)NSString *hobby;@end

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


0 0
原创粉丝点击