iOS学习 UITableViewCell 三种定制方法
来源:互联网 发布:linux 浏览网页 编辑:程序博客网 时间:2024/06/05 02:37
@implementation HomeTableViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];
switch (indexPath.row) {
case 0:
cell.textLabel.text =@"default Cell";
break;
case 1:
cell.textLabel.text =@"1 concentView";
break;
case 2:
cell.textLabel.text =@"2 Nib";
break;
case 3:
cell.textLabel.text =@"3 custom Cell";
break;
default:
break;
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailTableViewController *detailTVC = [[DetailTableViewControlleralloc]init];
//记录选中的内容
detailTVC.cellType = indexPath.row;
[self.navigationControllerpushViewController:detailTVC animated:YES];
}
#import <UIKit/UIKit.h>
#import "HomeTableViewController.h"
#import "MyCell.h"
//添加玫举
typedef enum kTableViewCellType{
kDefualtCellType = 0,
kConcentCellType = 1,
kNibCellType = 2,
kCustomCellType = 3
} kTableViewCellType;
@interface DetailTableViewController : UITableViewController
{
@private
NSArray *_listArray;
}
@property (nonatomic,assign)UITableViewCellStyle cellType;
@end
@implementation DetailTableViewController
- (void)viewDidLoad {
[superviewDidLoad];
_listArray = [UIFontfamilyNames];
self.tableView.rowHeight =60 ;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// 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 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier =@"cell";
if (self.cellType == kConcentCellType )
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
//第一种定制方法
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(20,10, 200, 30)];
label.backgroundColor = [UIColorcyanColor];
label.tag = 101;
[cell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[cell.contentViewviewWithTag:101];
label.text = _listArray[indexPath.row];
return cell;
} else if (self.cellType ==kNibCellType )
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibs = [[NSBundlemainBundle]loadNibNamed:@"MyView"owner:selfoptions:nil];
cell = [nibs objectAtIndex:0];
}
//第二种定制方法
UILabel *label = (UILabel *)[cell.contentViewviewWithTag:201];
label.text = _listArray[indexPath.row];
UIImageView *imgView = (UIImageView *)[cell.contentViewviewWithTag:202];
imgView.backgroundColor = indexPath.row %2 ==0 ?[UIColoryellowColor]:[UIColorredColor];
return cell;
}else if (self.cellType ==kCustomCellType )
{
//第三种定制方法,单独创建cell类
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[MyCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
}
//给MyCell类中的text赋值
cell.text = _listArray[indexPath.row];
return cell;
}else{
return nil;
}
}
#import <UIKit/UIKit.h>
@interface MyCell :UITableViewCell
{
@private
UILabel *_label;
}
@property (nonatomic,copy)NSString *text;
@end
#import "MyCell.h"
//声明私有方法,方便阅读
@interface MyCell()
- (void)initSubViews;
@end
@implementation MyCell
- (void)awakeFromNib {
// Initialization code
}
//重写init方法
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initSubViews]; //调用方法
}
return self;
}
//定制Cell内容
- (void)initSubViews{
_label = [[UILabelalloc]initWithFrame:CGRectZero];
_label.backgroundColor = [UIColorcyanColor];
[self.contentViewaddSubview:_label];
}
//改变系统样式位置
- (void)layoutSubviews{
[superlayoutSubviews]; //一定要调用父类,否则无法排版
//修改label位置
_label.frame =CGRectMake(80,10, 200, 40);
//给label引用赋值,注意位置
_label.text =self.text;
}
- iOS学习 UITableViewCell 三种定制方法
- 自定义UITableviewCell的三种方法
- 三种UITableViewCell刷新的方法
- 三种自定义UITableViewCell的方法
- 方法定制iOS学习笔记8-UITableView的定制
- 定制UITableViewCell
- 定制UITableViewCell
- UITableViewCell定制
- UITableViewCell定制
- iOS:UITableViewCell 方法 属性 详解
- IOS学习之自定义UITableViewCell
- [iOS开发必备技巧之]使用IB定制UITableViewCell
- iPhone开发学习笔记008——定制标题三种方法
- 学习。。。python定制方法
- UITableViewCell三种自定义方式
- UITableViewCell定制总结
- UITableViewCell accessory 定制
- iOS UITableViewCell 几个方法的优先级
- 虚拟存储器之地址翻译
- 秒杀系统架构分析与实战
- TCP的三次握手和四次撒手
- java 多线程实现生产者消费者模型
- STM32_SPI_笔记
- iOS学习 UITableViewCell 三种定制方法
- 虚拟存储器作为存储管理工具
- Nginx + CGI/FastCGI + C/Cpp
- FTP服务器的安装,配置,虚拟用户的创建
- 在Linux CentOS 6.5上安装python2.7.9
- Huge Page 是否是拯救性能的万能良药?
- 代码重构思考
- B. Making a String【水题】
- 安卓开发:继承View实现自定义View