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 = 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.

于 2012-11-22T01:33:47.923 に答える
0

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

于 2012-11-22T01:33:28.890 に答える
0

使用するだけです:

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