2012-09-07 56 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; 
} 

希望的細胞這有助於