#import "DiseaseListCell.h"
@implementation DiseaseListCell
@synthesize titleLabel;
@synthesize cellBackgroundImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
titleLabel.highlightedTextColor = [UIColor blackColor];
cellBackgroundImage = [[UIImageView alloc] init];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//我自定义的cell在点击的时候没显示选中的效果,应该是在这里需要进行设置
}
- (void)layoutSubviews
{
[super layoutSubviews];
cellBackgroundImage.frame = CGRectMake(0, 0, 320, 44);
cellBackgroundImage.backgroundColor = [UIColor clearColor];
cellBackgroundImage.image = [UIImage imageNamed:@"DiseaseListCell.png"];
[self.contentView addSubview:cellBackgroundImage];
titleLabel.frame = CGRectMake(15, 5, 260.0, 30);
titleLabel.font= [UIFont systemFontOfSize:22];
titleLabel.textAlignment = UITextAlignmentLeft;
titleLabel.textColor =
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
[self.contentView addSubview:titleLabel];
}
- (void)dealloc {
[titleLabel release];
[cellBackgroundImage release];
}
@end
//在viewController中用到自定义cell时,在方法中如下声明引入
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//....接下来就是给cell中各个元素赋值了
//....
}
selectedRow = 0;
//selectedRow用于判断cell是否应该进行扩展,初始化为0可以使初始化时第一行扩展显示,如果开始时都不需要扩展,可以设为-1
//heightForRowAtIndexPath这个方法在cellForRowAtIndexPath之前运行
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row != selectedRow) return CellHeight;//CellHeight是预定义的内容
else{
//这里进行cell内容的获取,并且计算高度
NSString *infoString = [[diseaseDetailArray objectAtIndex:indexPath.row] objectForKey:@"info"];
CGSize constraint = CGSizeMake(280, 20000);
CGSize size = [infoString sizeWithFont:[UIFont systemFontOfSize:16]
constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, CellHeight);
return height + (5 * 2) + CellHeight;
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
//...加载cell内容
//...
if (indexPath.row != selectedRow){
detailCell.cellBackgroundImage.frame = CGRectMake(0, 0, MainWindowWidth, CellHeight);
detailCell.cellBackgroundImage.image = [UIImage imageNamed:@"DiseaseDetailCell.png"];
detailCell.infoLabel.text = nil;
}
else {
//与上面确定高度时类似
NSString *infoString = [[diseaseDetailArray objectAtIndex:indexPath.row] objectForKey:@"info"];
CGSize constraint = CGSizeMake(280, 20000);
CGSize size = [infoString sizeWithFont:[UIFont systemFontOfSize:16]
constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[detailCell.infoLabel setText:infoString];
[detailCell.infoLabel setFrame:CGRectMake(20, 44, 280, MAX(size.height, 44))];
detailCell.cellBackgroundImage.frame = CGRectMake(0, 0, MainWindowWidth, MAX(size.height, 44) + 54);
detailCell.cellBackgroundImage.image = [UIImage imageNamed:@"DetailViewBng.png"];
//==============================================================================================
return detailCell;
}