7

全部,

我对iphone编程很陌生。在下面的代码中,我希望文本显示注释标签中的所有文本,但现在它正在截断它。numberofLines 也无法正常工作。现在它正在这样做。“我的名字是 Fred,我还没有死……”但我希望它显示全文“我的名字是 Fred,我还没有死,所以让我活着”,即使它必须在多行上。

- (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;
}
4

6 回答 6

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];

有关更多详细信息,请参阅我的博客以及链接中的这篇文章

于 2013-02-19T05:25:44.817 回答
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];
于 2013-10-07T10:29:21.880 回答
0

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

于 2013-02-19T05:15:55.080 回答
0

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

para.numberoOfLines = 10;

并尝试改变para.lineBreakMode

于 2013-02-19T05:25:33.677 回答
0

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

于 2013-02-19T05:25:41.057 回答
0

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

试试这个代码,

lblComment.numberofLines = 2; 
[lblComment setFrame:CGRectMake(100,20,200,70)];
于 2013-02-19T05:30:39.047 回答