UILabel的numberOfLines在单元格内不起作用

时间:2013-02-19 05:09:59

标签: iphone ios uitableview uilabel ios6

所有

我是iphone编程的新手。在下面的代码中,我希望文本显示注释标签中的所有文本,但现在它正在截断它。 numberofLines也不能正常工作。现在它正在这样做。 "我的名字是弗雷德,我死了......"但是我想要它显示全文"我的名字是弗雷德,我还没死,所以让我活着#34;即使它必须在多行上。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;

cell = [self.allCells objectForKey:[NSNumber numberWithInt:indexPath.row]];

if(!cell)
{
    cell = [[[NSBundle mainBundle] loadNibNamed:@"UserCell2" owner:nil options:nil] lastObject];
    cell.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [self.allCells setObject:cell forKey:[NSNumber numberWithInt:indexPath.row]];
}

GSAsynImageView *imgView = (GSAsynImageView*)[cell viewWithTag:1000];
UILabel *lblTitle = (UILabel*)[cell viewWithTag:1001];
UILabel *lblComment = (UILabel*)[cell viewWithTag:1003];
UILabel *lbltime = (UILabel*)[cell viewWithTag:1004];

//lblComment setLineBreakMode:NSLineBreakByWordWrapping];
//lblComment.numberOfLines = 0;
//lblComment.lineBreakMode = UILineBreakModeCharacterWrap;

if(self.arrComments.count==0)
{
    imgView.hidden = YES;
    lblTitle.text = nil;
    lblComment.text = nil;
    lbltime.text = nil;
    if(indexPath.row==1)lblTitle.text = @"No comments yet";
}
else
{
    imgView.hidden = NO;

    NSDictionary *dcUser = [self.arrComments objectAtIndex:indexPath.row];

    NSString *strBio = [dcUser objectForKey:@"CommentTxt"];
    NSString *strDisplayName = [dcUser objectForKey:@"CommenterDisplayName"];

    NSString *imgName = [dcUser objectForKey:@"ImageName"];
    NSString *usernamex = [dcUser objectForKey:@"CommenterUserName"];

    if([imgName isKindOfClass:[NSString class]])
    {
        if([imgName rangeOfString:@"facebook"].location!=NSNotFound || [imgName rangeOfString:@"twimg"].location!=NSNotFound)
            [imgView loadImageFromPath:imgName];
        else
            [imgView loadImageFromPath:[NSString stringWithFormat:@"%@images/%c/%@/50x50%@",WEBSERVER,[usernamex characterAtIndex:0],usernamex,imgName]];
    }

    lblTitle.text = strDisplayName;
    lblComment.text = strBio;
    lbltime.text = [self getDateTitle:[dcUser objectForKey:@"Date"]];

}

return cell;
}

6 个答案:

答案 0 :(得分:4)

尝试下面的代码并添加到您的单元格中。

UILabel * lblTitle = [[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(110, 31, 200, 50)];        
lblTitle.text = @"your Text ";
lblTitle.lineBreakMode = UILineBreakModeWordWrap;// add this line 
lblTitle.numberOfLines = 0;// add this  line
lblTitle.font = [UIFont fontWithName:@"Helvetica" size:12];

有关详细信息,请参阅我的博客,其中包含THIS链接

中的这篇文章

答案 1 :(得分:1)

试试这个......

UILabel * label = [[UILabel alloc]init];
    [label setFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
    label.text = @" Text to be displayed in label ";
    label.lineBreakMode = UILineBreakModeWordWrap ;// Wrap at word boundaries
    label.numberOfLines = 0;// this line include multiple lines
    [cell addSubview:label];

答案 2 :(得分:0)

我认为这可能是因为您想在标签上显示的字符串大小超过该标签的大小

答案 3 :(得分:0)

您可以使用UILabel的属性来增加行数。 与UILabel* para = [[UILabel alloc]init];

一样
para.numberoOfLines = 10;

并尝试更改para.lineBreakMode

答案 4 :(得分:0)

最简单的方法是使用storyboard或xib。您可以将标签(以及您想要的任何其他内容)添加到您拥有表格视图时自动获得的自定义单元格中。确保您的标签具有特定的宽度设置,并且它对单元格的顶部和底部有约束(确保将行数设置为0)。如果你有这些标签,标签将随着你在tableView中设置的单元格的高度而扩展:heightForRowAtIndexPath:。

答案 5 :(得分:0)

增加标签的高度,并将特定标签的行数设为2。

试试这段代码,

lblComment.numberofLines = 2; 
[lblComment setFrame:CGRectMake(100,20,200,70)];