0

我尝试按照此说明以编程方式设置 UITableView,但单元格的高度未更改。这是指令的链接。

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

我稍微修改了代码:

 - (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;
}

这是我得到的:

在此处输入图像描述

4

3 回答 3

1
  1. self.myTB.delegate = 自我;

  2. 修改以下代码以设置单个单元格的高度。

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

现在您将其设置为 100,因此高度不会改变。

于 2012-11-22T01:33:47.923 回答
0

我认为您不需要使用框架初始化 UITableViewCell 。我认为只是 alloc] init] autorelease] 应该可以解决问题。

于 2012-11-22T01:33:28.890 回答
0

只需使用:

self.myTB.rowHeight = 100;
于 2012-11-22T02:01:07.993 回答