UI09_UITableView自定义写cell

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.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];

    RootViewController *rootVC = [[RootViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
    self.window.rootViewController = naVC;
    [rootVC release];
    [naVC release];



    return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#import "RootViewController.h"
    引头文件
#import "MyCell.h"
#import "YourCell.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@end

@implementation RootViewController

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

    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 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0) {
        static NSString *yourReuse = @"reuse";
        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:yourReuse];
        if (!cell) {
            cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:yourReuse] autorelease];

        }
        cell.label.text = [NSString stringWithFormat:@"%ld", indexPath.row];
        return cell;

    } else {
#warning 即使是在同一个tableView显示, 不同的cell必须要指定不同的重用标志, 否则以重用标志找cell的时候, 可以找到的cell类型有误, 造成崩溃
        static NSString *reuse = @"yourReuse";
        YourCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[YourCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
        }
        cell.leftLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
        return cell;
    }

}

YourCell.h

#import <UIKit/UIKit.h>

@interface YourCell : UITableViewCell

@property(nonatomic, retain)UIImageView *myImageView;
@property(nonatomic, retain)UILabel *leftLabel;
@property(nonatomic, retain)UILabel *rightLabel;

@end

YourCell.m

#import "YourCell.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation YourCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView {
    self.myImageView = [[UIImageView alloc] init];
    self.myImageView.backgroundColor = [UIColor purpleColor];
    [self.contentView addSubview:self.myImageView];
    [self.myImageView release];

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

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

- (void)layoutSubviews {
    [super layoutSubviews];

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

}

MyCell.h

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell

@property(nonatomic, retain)UILabel *label;
@property(nonatomic, retain)UIImageView *leftImageView;
@property(nonatomic, retain)UIImageView *centerImageView;
@property(nonatomic, retain)UIImageView *rightImageView;
#warning 给自定义的cell写属性的时候, 要注意一定要和系统提供的三个控件名进行区分, imageView,  textLabel, detailTextLabel, 尤其是imageView, 一定不能写
@end

MyCell.m

#import "MyCell.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation MyCell
//  在自定义cell里需要写两部分内容
#pragma mark    第一部分是重写cell的初始化方法, 在这个方法中我们创建属性的视图, 不设置尺寸
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView {
    //
    self.label = [[UILabel alloc] init];
    self.label.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:self.label];
    [_label release];

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

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

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

#pragma mark    第二部分, 在这个方法里设置所有子视图的尺寸, 而且这个方法是cell出现之前执行的最后一个方法
- (void)layoutSubviews {
    //  这句话一定不能忘写
    [super layoutSubviews];

    //  设置所有子视图的坐标和大小
    self.label.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 2);
    self.leftImageView.frame = CGRectMake(0, HEIGHT / 2, WIDTH / 3, HEIGHT / 2);
    self.centerImageView.frame = CGRectMake(WIDTH / 3, HEIGHT / 2, WIDTH / 3, HEIGHT / 2);
    self.rightImageView.frame = CGRectMake(WIDTH / 3 * 2, HEIGHT / 2, WIDTH / 3, HEIGHT / 2);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值