首页 > 代码库 > 单元格的三种定制方式
单元格的三种定制方式
AppDelegate.m
MainViewController *mainCtrl = [[MainViewController alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:mainCtrl]; self.window.rootViewController = navCtrl; [mainCtrl release]; [navCtrl release];
MainViewController.m
#import "MainViewController.h" #import "FirstViewController.h" #import "SecondViewController.h" #import "ThirdViewController.h" @interface MainViewController () @end @implementation MainViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { self.title = @"单元格定制方式"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //设置视图回来以后是否存在选中效果,默认是yes // self.clearsSelectionOnViewWillAppear = NO; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *iden = @"cell_Root"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden]; } cell.textLabel.text = [NSString stringWithFormat:@"第%d中单元格定制方式",indexPath.row+1]; return cell; } //点击单元格相应的协议方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //点击第一个单元格相应事件 if (indexPath.row == 0) { FirstViewController *firstCtrl = [[FirstViewController alloc] init]; [self.navigationController pushViewController:firstCtrl animated:YES]; [firstCtrl release]; }else if (indexPath.row == 1) { SecondViewController *secondCtrl = [[SecondViewController alloc] init]; [self.navigationController pushViewController:secondCtrl animated:YES]; [secondCtrl release]; }else if (indexPath.row == 2) { ThirdViewController *thirCtrl = [[ThirdViewController alloc] init]; [self.navigationController pushViewController:thirCtrl animated:YES]; [thirCtrl release]; } } @end----------------------------第一种制定方式[系统内置]-------------------------------
FirstViewController.h
@interface FirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property(nonatomic, retain)NSArray *data;
FirstViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"第一种单元格定制方式"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //创建表视图 UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain]; tableView.delegate = self; tableView.dataSource = self; [self.view addSubview:tableView]; [tableView release]; //读取文件 NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"]; _data = http://www.mamicode.com/[[NSArray alloc] initWithContentsOfFile:path];>-----------------------------第二种制定方式[Nib文件]-------------------------------
SecondViewController.h@interface SecondViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property(nonatomic, retain)NSArray *data;SecondViewController.m#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"单元格第二种定制方式"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //创建表视图 UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain]; tableView.delegate = self; tableView.dataSource = self; [self.view addSubview:tableView]; [tableView release]; //读取文件 NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"]; _data = http://www.mamicode.com/[[NSArray alloc] initWithContentsOfFile:path];>------------------------------------------第三种制定方式[自定义单元格]----------------------------
ThirdViewController.h@interface ThirdViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property(nonatomic, retain)NSMutableArray *data;ThirdViewController.m#import "ThirdViewController.h" #import "NewsModel.h" #import "NewsCell.h" @interface ThirdViewController () @end @implementation ThirdViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"单元格第三种定制方式"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //加载数据 NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"]; NSArray *arrary = [[NSArray alloc] initWithContentsOfFile:path]; _data = http://www.mamicode.com/[[NSMutableArray alloc] init];>NewsCell.h@class NewsModel; @interface NewsCell : UITableViewCell { UILabel *_titleLabel; UILabel *_commentLabel; UILabel *_timeLablel; } @property(nonatomic, retain)NewsModel *model;NewsCell.m
#import "NewsCell.h" #import "NewsModel.h" @implementation NewsCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code [self _initView]; } return self; } - (void)_initView { //创建显示标题的label // _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 20)]; _titleLabel = [[UILabel alloc] init]; _titleLabel.backgroundColor = [UIColor clearColor]; _titleLabel.font = [UIFont boldSystemFontOfSize:16]; [self.contentView addSubview:_titleLabel]; //创建显示评论数的label // _commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 150, 20)]; _commentLabel = [[UILabel alloc] init]; _commentLabel.backgroundColor = [UIColor clearColor]; _commentLabel.textColor = [UIColor grayColor]; _commentLabel.font = [UIFont boldSystemFontOfSize:13]; [self.contentView addSubview:_commentLabel]; //创建显示发表时间的label // _timeLablel = [[UILabel alloc] initWithFrame:CGRectMake(200, 25, 100, 20)]; _timeLablel = [[UILabel alloc] init]; _timeLablel.backgroundColor = [UIColor clearColor]; _timeLablel.textColor = [UIColor grayColor]; _timeLablel.font = [UIFont systemFontOfSize:13]; [self.contentView addSubview:_timeLablel]; } /* 视图将要显示的时候调用 */ /* 1.布局,设置frame 2.添加数据 */ - (void)layoutSubviews { [super layoutSubviews]; //布局 _titleLabel.frame = CGRectMake(10, 5, 300, 20); _commentLabel.frame = CGRectMake(10, 25, 150, 20); _timeLablel.frame = CGRectMake(200, 25, 100, 20); //添加数据 _titleLabel.text = _model.title; _commentLabel.text = [NSString stringWithFormat:@"评论数%@条",_model.commentCount]; _timeLablel.text = [NSString stringWithFormat:@"%@小时前",_model.time]; } @end
NewsModel.h@interface NewsModel : NSObject @property(nonatomic, copy)NSString *title; //标题 @property(nonatomic, copy)NSString *commentCount; //评论数 @property(nonatomic, copy)NSString *time; //时间
NewsModel.m- (void)dealloc { [_commentCount release]; [_title release]; [_time release]; [super dealloc]; }
单元格的三种定制方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。