2012-09-07 29 views
0

我应用程序中的UILabel表现异常,我想知道是否有人对如何解决它有任何想法或建议。UILabel - WordWrap - Frame问题

1.首先我设置标签的框架,它们都出现在tableView的单元格内。

CGRect CellFrame = CGRectMake(0, 0, 300, 75); 
CGRect Label1Frame = CGRectMake(10, 5, 290, 20); 
CGRect Label2Frame = CGRectMake(0, 20, 290, 50); 
UILabel *lblTemp; 

UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CellFrame  reuseIdentifier:cellIdentifier]; 
  1. 然后我设置标签

的性质1.

lblTemp = [[UILabel alloc] initWithFrame:Label1Frame]; 
lblTemp.tag = 1; 
lblTemp.font = [UIFont fontWithName:@"Helvetica" size:12.0]; 
lblTemp.textColor = [UIColor blackColor]; 
lblTemp.lineBreakMode = UILineBreakModeWordWrap; 
lblTemp.numberOfLines = 0; 
[cell.contentView addSubview:lblTemp]; 

//Initialize Label with tag 2. 
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame]; 
lblTemp.tag = 2; 
lblTemp.font = [UIFont fontWithName:@"Helvetica" size:10]; 
lblTemp.textColor = [UIColor lightGrayColor]; 
lblTemp.lineBreakMode = UILineBreakModeWordWrap; 
lblTemp.numberOfLines = 0; 
[cell.contentView addSubview:lblTemp]; 

return cell; 

3.Then我设置小区

//set up the cell 

UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1]; 
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2]; 

CaseFeed *aCaseFeed = [[CaseFeed alloc] init]; 
aCaseFeed = [CaseFeedbook objectAtIndex:indexPath.row]; 

lblTemp1.text = [NSString stringWithFormat:@"%@", aCaseFeed.title]; 
lblTemp2.text = [NSString stringWithFormat:@"%@", aCaseFeed.description]; 

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

return cell; 

问题的内容: lblTemp2的内容(th e单元格中的第二个标签)将跨越2行或更多行。在运行期间,当填充lblTemp2的文本时,前两行显示完美,但第三行在框架的左侧外部开始。

有关如何解决此问题的任何想法?这是为什么发生?

+0

[@ehul(http://stackoverflow.com/users/1354417/ehul)弄来你的问题解决了.. –

回答

0

使用下面的代码来设置你的一个的tableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    else 
    { 
     for (id v in cell.contentView.subviews) 
     { 
      UIView *realV = (UIView *) v; 
      [realV removeFromSuperview]; 
     } 
    } 
    CGRect CellFrame = CGRectMake(0, 0, 300, 75); 
    CGRect Label1Frame = CGRectMake(10, 5, 290, 20); 
    CGRect Label2Frame = CGRectMake(0, 20, 290, 50); 


    UILabel *label1 = [[UILabel alloc] init]; 
    label1.frame = Label1Frame; 
    label1.tag = 1; 
    label1.font = [UIFont fontWithName:@"Helvetica" size:12.0]; 
    label1.textColor = [UIColor blackColor]; 
    label1.lineBreakMode = UILineBreakModeWordWrap; 
    label1.numberOfLines = 0; 
    [cell.contentView addSubview:label1]; 

    UILabel *label2 = [[UILabel alloc] init]; 
    label2.frame = Label2Frame; 
    label2.tag = 2; 
    label2.font = [UIFont fontWithName:@"Helvetica" size:10.0]; 
    label2.textColor = [UIColor lightGrayColor]; 
    label2.lineBreakMode = UILineBreakModeWordWrap; 
    label2.numberOfLines = 0; 
    [cell.contentView addSubview:label2]; 

    CaseFeed *aCaseFeed = [[CaseFeed alloc] init]; 
    aCaseFeed = [CaseFeedbook objectAtIndex:indexPath.row]; 

    label0.text = [NSString stringWithFormat:@"%@", aCaseFeed.title]; 
    label1.text = [NSString stringWithFormat:@"%@", aCaseFeed.description]; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    return cell; 
} 

希望的细胞这有助于