如何根据其中的内容量缩放UITableViewCell
?在我的单元格中,我使用三个代表论坛的标签。标签名为“别名”,“日期”和“评论”。第三个标签,注释,可以是任意数量的行。因此,我需要我的单元格变为动态大小,具体取决于“comments”标签中的文本数量。这是我的代码:
- (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;
}
答案 0 :(得分:1)
查看本教程中的how to set dynamically set cell height,
基本上你需要使用这样的方法来计算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;
}