动态UITableViewCell高度

我试图让我的单元格根据单元格中的文本数量动态地改变高度。目前我有单词包装..但是一旦单元格有很多内容(如果它转到第3行) )你看不到第二行的任何东西。

这就是我到目前为止…希望有人可以看到我是否遗漏了某些东西或做错了什么……任何帮助都会受到赞赏。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UILabel *label = nil; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 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]; } // //Display cells with data that has been sorted from startSortingTheArray NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]; NSString *key = [keys objectAtIndex:indexPath.row]; CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; if (!label) label = (UILabel*)[cell viewWithTag:1]; [label setText:key]; [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))]; //Applise current key value to cell text label //cell.textLabel.text = key; return cell; } //Cell size for word wrapping. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; { //Display cells with data that has been sorted from startSortingTheArray NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]; NSString *key = [keys objectAtIndex:indexPath.row]; NSLog(@"%@", key); CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; CGFloat height = MAX(size.height, 44.0f); return height + (CELL_CONTENT_MARGIN * 2); } 

更新上面的代码现在是一个工作版本..反正我:)

重新开始并逐字逐句地学习本教程,你会没事的: Cocoa是我的女朋友

它实际上看起来像你看了这个教程,只是它的一部分。 我仍然没有看到你设置标签的框架。