DavidNg
DavidNg

Reputation: 2836

iOS: unable to change the height of UITableViewCell

I tried to follow this instruction to setup UITableView programatically, but the height of cell is unchanged. Here is the link for the instruction.

http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

I have modified the code a little bit:

 - (void)viewDidLoad{
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0) style:UITableViewStylePlain];
    self.myTB = tableView;
    [tableView release];    
    UIView *view = [[UIView alloc] init];
    [view addSubview:self.myTB];
    self.view = view;
    [view release];
    self.myTB.dataSource = self;

    items= [[NSMutableArray alloc] init];
    [items addObject:@"Happiness is having a large, loving, caring, close-knit family in another city.\n\n\t\t-George Burns (1896 - 1996)"];
    [items addObject:@"When I am abroad, I always make it a rule never to criticize or attack the government of my own country. I make up for lost time when I come home.\n\n\t\t-Sir Winston Churchill (1874 - 1965)"];
    [items addObject:@"After two years in Washington, I often long for the realism and sincerity of Hollywood.\n\n\t\t-Fred Thompson, Speech before the Commonwealth Club of California"];
    [items addObject:@"It is a profitable thing, if one is wise, to seem foolish.\n\n\t\t-Aeschylus (525 BC - 456 BC)"];
    }
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
    return [items count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell;
    UILabel *label = nil;
    cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil){
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero ] autorelease];
        label = [[UILabel alloc] initWithFrame:CGRectZero];
        [label setLineBreakMode:UILineBreakModeWordWrap];
        [label setMinimumFontSize:FONT_SIZE];
        [label setNumberOfLines:0];
        [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
        [label setTag:1];
        [[cell contentView] addSubview:label];
    }
    NSString *text = [items objectAtIndex:[indexPath row]];
    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    if (!label)
        label = (UILabel*)[cell viewWithTag:1];
    [label setText:text];
    [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
    return cell;
}

Here is what I get:

enter image description here

Upvotes: 0

Views: 804

Answers (3)

meim
meim

Reputation: 740

  1. self.myTB.delegate = self;

  2. Modify the following code to set the heights for individual cells.

    • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 100; }

Now you set it as 100 constantly, so the height doesn't change.

Upvotes: 1

Nic Hubbard
Nic Hubbard

Reputation: 42139

Just use:

self.myTB.rowHeight = 100;

Upvotes: 0

atreat
atreat

Reputation: 4403

I don't think you need to init the UITableViewCell with a frame. I think just alloc] init] autorelease] should do the trick.

Upvotes: 0

Related Questions