在我的项目中调整单元格大小

时间:2012-03-09 13:37:25

标签: xcode

我有这个表有自定义单元格,如何根据内容调整cal单元格?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomTableCell";
    static NSString *CellNib = @"DetailViewCell";


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

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (DetailViewCell *)[nib objectAtIndex:0];
    }   

    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.cellTitleLabel.textColor = [UIColor blackColor];
    cell.cellSubtitleLabel.textColor = [UIColor darkGrayColor];

    informations = [[NSArray alloc] initWithObjects:titleString, subtitleString, stateString, categoryString, populationString, nil];
    subtitles = [[NSArray alloc] initWithObjects:@"City", @"Country", @"State", @"Category", @"Population", nil];

    cell.cellTitleLabel.text = [informations objectAtIndex:indexPath.row];
    cell.cellSubtitleLabel.text = [subtitles objectAtIndex:indexPath.row];

    return (DetailViewCell *) cell; 
} 

这是DetailViewCell.m

#import "DetailViewCell.h"

@implementation DetailViewCell

@synthesize cellTitleLabel;
@synthesize cellSubtitleLabel;

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

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code.
    }
    return self;
}

- (void)dealloc {
    [cellTitleLabel release];
    [cellSubtitleLabel release];
    [super dealloc];
}

谢谢!

1 个答案:

答案 0 :(得分:0)

更改单元格内容后,请在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath中重置单元格高度,然后调用[cell setNeedsDisplay];再次绘制单元格。