IOS代码布局(四) UITableView & UITableViewCell
(一)TableView
1、定义
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, rect.size.width,srcHeight-100)];;
2、设置数据源及委托
tableView.dataSource = self; tableView.delegate = self;
注:此时要在viewController.h中加入
<UITableViewDelegate,UITableViewDataSource>
例:
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> { NSMutableArray *items ; }
3、设置高度
tableView.rowHeight = 60;
4、加入视图
[self.view addSubview:tableView];
5、刷新表单
[tableView reloadData];
6、分区(section)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 4; //设置四个分区 } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) { return 0; //第一个分区宽度为0 } return 20; //其余分区宽度为20 }
7、表单数目
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return numOfCell; }
8、表单样式
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CIdentifier = @"CellIdentifier"; TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CIdentifier];
//注:(坑点之一)这里将声明应该写在此处,而不能写成全局变量。此句的意思是寻找有没有 可用的indetifier为TryIdentifier的cell,如果没有下面走==nil的判断,证明当前没有可用的,如果有则cell!=nil,无需重新init。如果写成全局变量相当于全局只找一次有没有identifier为TryIdentifier的,即只用一个cell
//如果此处采用不同的cidentifire,倘若有100个cell,则会加载100个cell,然后复用。如果采用的是一样的cidentifier,则只回会加载5个cell(屏幕容纳最大范围),第六个就开始服用,因此需要每次都赋值进行覆盖(在if cell=nil 下面)。倘若不覆盖则会采用之前的图片不清除
cell.selectionStyle=UITableViewCellSelectionStyleNone;
//取消选中cell的高亮样式
if (cell == nil) { cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CIdentifier];
}
[cell setPhoto2:img];
return cell;
}
(二)TableViewCell
1、cell类
cell.h #import <UIKit/UIKit.h> @interface cell : UITableViewCell { UIImage *headImg; UILabel *name; UILabel *audienceNum; } @end
cell.m #import "cell.h" @implementation cell -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { orderLabel =[[UILabel alloc]initWithFrame:CGRectMake(15, 20, 21, 20)]; [self.contentView addSubview:orderLabel]; orderImage=[[UIImageView alloc]initWithFrame:CGRectMake(15,20,20,20)]; [self.contentView addSubview:orderImage]; headImage =[[UIImageView alloc]initWithFrame:CGRectMake(50,10,40,40)]; [self.contentView addSubview:headImage]; ... } return self; } -(void)setOrderLabel:(NSString *)text { orderLabel.textAlignment = NSTextAlignmentCenter; //居中设置 orderLabel.textColor=[UIColor grayColor]; //颜色设置 orderLabel.text=text; } -(void)setOrderImage:(UIImage *)img { orderImage.image = img; } -(void)setHeadImage:(UIImage *)img { headImage.layer.cornerRadius=20; headImage.layer.masksToBounds=YES; //告诉layer将位于它之下的layer都遮盖住 headImage.image = img; } @end
2、右侧小箭头(在上面表单样式中)
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
注:若想显示不同样式,在表单样式中用不同cell样式定义即可,例:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row<3) {
tryCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TryIdentifier forIndexPath:indexPath];
//使用indexPath确定表正在请求单元的哪一行,然后使用该行的值为请求的行获取正确字典
NSDictionary *rowData = self.computers[indexPath.row];
UIImage *image = [UIImage imageNamed:@"star"];
cell.myImage=image;
//使用获取的数据填充单元
cell.ownName=rowData[@"Name"];
return cell;
}else
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CIdentifier forIndexPath:indexPath];
NSDictionary *rowData = self.computers[indexPath.row];
cell.ownName = rowData[@"Name"];
return cell;
}
}