UI09_UITableView自定义写cell
2015-09-28 08:52
387 查看
AppDelegate.h
AppDelegate.m
RootViewController.h
RootViewController.m
YourCell.h
YourCell.m
MyCell.h
MyCell.m
#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); }
相关文章推荐
- UIActionSheet 操作表
- UI08_UITableView界面传值
- UITableView
- Number Sequence(快速幂矩阵)
- 201509280825_《css3——media query整理2》
- AlgorithmsI PA2: Randomized Queues and Deques Subset
- AlgorithmsI PA2: Randomized Queues and Deques RandomizedQueue
- AlgorithmsI PA2: Randomized Queues and Deques Deque
- iOS开发小白学习体验-UICollectionView(一)
- [iOS]iOS中UITabbarController左右滑动切换
- iOS开发UI篇—UIScrollView控件介绍
- string stringbuffer stringbuilder区别
- iOS9系列专题五——全新的联系人与联系人UI框架
- pat 1099 Build A Binary Search Tree
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- 欧拉回路 (Euler Circuit) POJ 1780
- 如何汉化EasyUI、ExtJS等富客户端框架
- HDU 5493 Queue (树状数组+二分)2015 ICPC 合肥网赛
- 设置UITableViewCell 选中时的背景颜色
- N-queens