您的位置:首页 > 产品设计 > UI/UE

定制UITableViewCell

2013-03-18 15:30 232 查看
定制是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,很简单的就实现了我们的需求。这个例子虽然比较简单,但是很好的展现了定制这种解决问题思路。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: