今天学习了系统cell和自定义的cell。并且使用了nib文件和代码两种方式建立cell。我就顺势来总结下cell吧。
从cell的类型来说,分 系统cellUITableVIewCell和自定义MyCell两种。
cell的加载方式来说,可以通过 xib文件、代码生成两种方式。
cell的显示方式来说:分为一般的统一显示和一个tableView两种不同的cell显示方式。
一。UITableViewCell的cell。
1.xib文件的加载,代码生成。由于系统的cell。上篇就介绍了系统cell的属性。直接给出协议的cell绘制方式吧
d
1.UITableViewCellStyleSubtitle 类型
2.
UITableViewCellStyleDefault
3.
UITableViewCellStyleValue1
4.
UITableViewCellStyleValue2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIndentifiter = @"CellIndentifiter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifiter];
}
cell.imageView.image = [UIImage imageNamed:@"1.png"];
cell.textLabel.text = @"cu1";
cell.detailTextLabel.text = @"detail tet1";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
二,自定义cell的加载。
1.xib文件的加载。
1)我们事先定义了cell的xib,h,m文件。
并在xib文件中绘制了cell的外观
2)下一步就是自定义cell的绘制啦
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIndentifiter = @"MyIndentifiter";
static BOOL yesOrNo = NO;
if (!yesOrNo) {
UINib *nib = [UINib nibWithNibName:@"LCTableVIewCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:cellIndentifiter];
}
LCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter];
cell.mLable1.text = @"lab1";
cell.mLable1.text = @"lab2";
return cell;
}
2.自定义cell的代码实现
1)自定义cell的内部实现就要看下面的这个方法啦,代码我就不写了。按照frame进行设置就ok
#import "LCTableViewCell.h"
@implementation LCTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
效果是相同的。
2)还有一种我们常见的办法是在我们的系统cell里面,当cell==nil的时候,进行自定义cell绘制。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIndentifiter = @"CellIndentifiter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter];
if (cell == nil) {
//cell的自定义绘制
}
//cell的属性设置
return cell;
}
三,下面就是两种不同的cell类型的穿插绘制勒
如图:
代码如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
NSLog(@"%d",row);
static NSString *cellIndentifiter = @"CellIndentifiter";
static NSString *cellIndentifiter1 = @"CellIndentifiter1";
row = row%2;
if (!row) {
UINib *nib = [UINib nibWithNibName:@"LCTableVIewCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:cellIndentifiter1];
LCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter1];
cell.mLable1.text = @"custom label1";
cell.mLabel2.text = @"custom label2";
return cell;
}
else
{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifiter];
cell.imageView.image = [UIImage imageNamed:@"1.png"];
cell.textLabel.text = @"cu1";
cell.detailTextLabel.text = @"detail tet1";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
}
交叉绘制的的注意点:
1)每一次的cell的绘制都是需要创建新的cell,为了避免两种不同的方式的交叉混乱的情况
2)对于什么时候绘制什么样的表格也需要设置清楚