定制UITableViewCell

定制是iphone开发中常用的方法之一,这里用定制UITableViewCell来介绍一下这种解决问题的思路。

下面是GRE作业中tableCell的效果图

可以看到,cell中用到的分割线和折叠指示图标,如果用SDK提供的标准UITableViewCell实现不了,采用定制cell的思路可以简单完美地解决这个问题。

下面是代码实现:

WordCell类

@interface WordCell : UITableViewCell {

UIImageView *wrapImgSquare; //折叠指示图标

UIImageView *separImgSquare; //分隔线

}

@property(nonatomic, retain)UIImageView *wrapImgSquare;

@property(nonatomic, retain)UIImageView *sepImgSquare;

WordList.m中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @”Cell”;

WordCell *cell = (WordCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[WordCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

}

// Configure the cell.

NSUInteger row = indexPath.row;

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.accessoryType = UITableViewCellAccessoryNone;

GREWord *cWord = [sWordList objectAtIndex:row];

cell.textLabel.text = cWord.sName;

cell.textLabel.textColor = HEXCOLOR(WORD_COLOR);

cell.detailTextLabel.textColor = HEXCOLOR(WORD_COLOR);

cell.detailTextLabel.numberOfLines = 3;

if (cWord.bExpanded)

{

cell.detailTextLabel.text = cWord.sDetail;

cell.wrapImgSquare.image = [UIImage imageNamed:@"table_cell_unwrapped.png"];

}

else

{

cell.detailTextLabel.text = nil;

cell.wrapImgSquare.image = [UIImage imageNamed:@"table_cell_wrapped.png"];

}

[cell setBackgroundColor:[UIColor clearColor]];

return cell;

}

WordCell继承了UITableViewCell,UITableViewCell是core,WordCell附加属性是shell。代码中用自定义的WordCell替换了SDK提供的

UITableViewCell *cell,很简单的就实现了我们的需求。这个例子虽然比较简单,但是很好的展现了定制这种解决问题思路。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值