1

如何UITableViewCell根据其中的内容量缩放 s?在我的单元格中,我使用了三个代表论坛的标签。这些标签被命名为“别名”、“日期”和“评论”。第三个标签,评论,可以是任意数量的行。因此,我需要我的单元格变成动态大小,具体取决于“评论”标签中的文本数量。这是我的代码:

- (BOOL)textFieldShouldReturn:(UITextField *)pTextField
{
[self setLoadingState:YES];
[pTextField resignFirstResponder];

NSUserDefaults *userStorage = [NSUserDefaults standardUserDefaults];

NSString *alias = [self urlEncode:[userStorage objectForKey:@"alias"]];
NSString *email = [self urlEncode:[userStorage objectForKey:@"email"]];
NSString *who = [self getUniqueDeviceId];
NSString *comment = [self urlEncode:[pTextField text]];

comment = [comment stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
who = [who stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if([self isBlank:comment])
{
    [self setLoadingState:NO];
    pTextField.text = @"";
    return NO;
}
if([self isBlank:alias])
{
    [self showMessagePopup:NSLocalizedString(@"MessageMustChooseAlias", nil)];
    return NO;
}

[self.forumThreadDataProvider startSendPost:self.taskId : self.forumThreadId : alias : who : email : comment];

pTextField.text = @"";

return YES;
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ForumthreadCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Feedback *item = [self.items objectAtIndex:indexPath.row];

UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];

commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];

return cell;
}

我已经尝试过使用以下代码示例,但它失败了:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];

if (cell) {        
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
return commentLabel.frame.size.height;
}  
else
return 30;
}
4

1 回答 1

1

查看本教程以了解如何设置动态设置单元格高度

基本上你需要使用这样的方法来计算 Laebl 的高度,

- (CGFloat)RAD_textHeightForSystemFontOfSize:(CGFloat)size {
    //Calculate the expected size based on the font and linebreak mode of the label
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 50;
    CGFloat maxHeight = 9999;
    CGSize maximumLabelSize = CGSizeMake(maxWidth,maxHeight);

    CGSize expectedLabelSize = [self sizeWithFont:[UIFont systemFontOfSize:size] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; 

    return expectedLabelSize.height;
}

然后你需要实现该heightForRowAtIndexPath方法,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {  
    NSString *label = [self.aNote length] == 0 ? kDefaultNoteLabel : self.aNote;
    CGFloat height = [label RAD_textHeightForSystemFontOfSize:kTextViewFontSize] + 20.0;
    return height;
}
于 2012-10-28T19:12:04.280 回答