自定义Cell与动态确定Cell高…

来源:互联网 发布:烙印战士 漫画软件 编辑:程序博客网 时间:2024/06/01 10:41
原文地址:自定义Cell与动态确定Cell高度作者:YANG
问题10.自定义Cell

以前用代码布局视图觉得麻烦的地方就是需要多次的运行程序来看各种控件摆放位置是否合适,所以往往偷懒试用xib进行视图的布局,这次为了方便移植一律使用代码来完成工作,因此接触到自定义cell的一些内容,在这做个笔记,将自定义的一个cell代码记下来

#import"DiseaseListCell.h"


@implementation DiseaseListCell


@synthesize titleLabel;

@synthesize cellBackgroundImage;


-(id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier {

    //自定义cell中的元素要在这里进行初始化

    self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

       titleLabel = [[UILabel alloc] init];

titleLabel.highlightedTextColor = [UIColor blackColor];

cellBackgroundImage = [[UIImageView alloc] init];

    }

    return self;

}


-(void)setSelected:(BOOL)selected animated:(BOOL)animated {

    

    [super setSelected:selectedanimated:animated];

    

    // Configure the view for theselectedstate.

//我自定义的cell在点击的时候没显示选中的效果,应该是在这里需要进行设置

}


-(void)layoutSubviews //在这里进行元素的详细设置

{

[superlayoutSubviews];

 

cellBackgroundImage.frame = CGRectMake(0, 0, 320, 44);

cellBackgroundImage.backgroundColor = [UIColor clearColor];

cellBackgroundImage.image = [UIImageimageNamed:@"DiseaseListCell.png"];

[self.contentView addSubview:cellBackgroundImage];

 

titleLabel.frame = CGRectMake(15, 5, 260.0, 30);

titleLabel.font= [UIFont systemFontOfSize:22];

titleLabel.textAlignment = UITextAlignmentLeft;

titleLabel.textColor =  [UIColor blackColor];

titleLabel.backgroundColor = [UIColor clearColor];

titleLabel.lineBreakMode = UILineBreakModeWordWrap;

[self.contentView addSubview:titleLabel];

}


- (void)dealloc {

[titleLabel release];

[cellBackgroundImage release];

    [super dealloc];

}


@end

//在viewController中用到自定义cell时,在方法中如下声明引入

 

 


-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString*CellIdentifier = @"Cell";

    DiseaseListCell *cell =(DiseaseListCell *)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil) {

       cell =[[[DiseaseListCell alloc]initWithStyle:UITableViewCellStyleDefault 

 reuseIdentifier:CellIdentifier] autorelease];

   }//使用xib做的cell如何引入我也给忘了。。还得查查。。

    //....

//....接下来就是给cell中各个元素赋值了

//....

    return cell;

}


问题11.动态确定Cell高度


 

selectedRow= 0;

//selectedRow用于判断cell是否应该进行扩展,初始化为0可以使初始化时第一行扩展显示,如果开始时都不需要扩展,可以设为-1

//heightForRowAtIndexPath这个方法在cellForRowAtIndexPath之前运行

- (CGFloat) tableView:(UITableView *)tableViewheightForRowAtIndexPath:(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:[UIFontsystemFontOfSize:16] 

constrainedToSize:constraintlineBreakMode:UILineBreakModeWordWrap];

CGFloat height =MAX(size.height, CellHeight);

return height + (5 *2) + CellHeight;

}


// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString*CellIdentifier = @"Cell";

    

    DiseaseDetailCell *detailCell= (DiseaseDetailCell *)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    if (detailCell == nil) {

       detailCell = [[[DiseaseDetailCell alloc]initWithStyle:UITableViewCellStyleDefault 

  reuseIdentifier:CellIdentifier]autorelease];

    }

//...

//...加载cell内容

//...

if (indexPath.row != selectedRow){

detailCell.cellBackgroundImage.frame = CGRectMake(0, 0,MainWindowWidth, CellHeight);

detailCell.cellBackgroundImage.image = [UIImageimageNamed:@"DiseaseDetailCell.png"];

detailCell.infoLabel.text = nil;

}

else {

//与上面确定高度时类似

NSString *infoString= [[diseaseDetailArray objectAtIndex:indexPath.row]objectForKey:@"info"];

CGSize constraint =CGSizeMake(280, 20000);

CGSize size =[infoString sizeWithFont:[UIFontsystemFontOfSize:16] 

constrainedToSize:constraintlineBreakMode: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 = [UIImageimageNamed:@"DetailViewBng.png"];

    }

//==============================================================================================

return detailCell;

}





原创粉丝点击