2012-11-27 69 views
0

我有一個列表視圖,有時會有長文本。當文本是幾段時,它往往會在某個時候被切斷。奇怪的行爲的一部分是它不總是在同一個字符上被截斷,而是隨機的。ios - 列表項在一定長度後有文本截斷

這裏是我用來填充列表代碼:

// CREATING EACH CELL IN THE LIST 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    //static const NSInteger kLabelTag = 1; 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) 
      reuseIdentifier:@"business"]; 

    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")]; 
    NSString *first_name = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"first_name")]; 
    //Boolean *is_private = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"is_private")]; 

    // Creating a constraint size for the label you are making 
    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

    // Determining the size that you will need for the label based on the comment 
    // length and the contraint size 
    CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    // Creating the label and initializing it with the frame that you have 
    // determined by your size calculations above 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, MAX(size.height, 44.0f) + 20.0f)]; 

    // setting up the label properties 
    label.numberOfLines = 0; 
    label.lineBreakMode = UILineBreakModeWordWrap; 

    standardUserDefaults = [NSUserDefaults standardUserDefaults]; 
    NSString *is_private = [standardUserDefaults objectForKey:@"is_private"]; 

    if (is_private == nil) 
    { 
     label.text = [first_name stringByAppendingString:[@": " stringByAppendingString:comment]]; // comment; 
    } 
    else 
    if ([is_private isEqualToString:@"0"]) 
    { 
     label.text = [first_name stringByAppendingString:[@": " stringByAppendingString:comment]]; // comment;  
    } 
    else 
    { 
     label.text = comment; 
    } 

    // adding the label view to the cell that you have created 
    [cell.contentView addSubview:label];   

    // CLOSE THE SPINNER 
    [spinner stopAnimating]; 

    // return the cell for the table view 
    return cell; 
} 

//This method will determine how tall each row needs to be. Cell size for word wrapping. 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{  
    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")]; 

    // Again you are getting the constraints because you are going to us this size 
    // to constrain the height of the cell just like you determined the size for the label. 
    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

    // Again, determining the size that we will need for the label, because it will drive 
    // the height of the cell 
    CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    // Finally, we can determine the height for the cell! 
    CGFloat height = MAX(size.height, 44.0f); 

    // return the height, which is the height of the label plus some extra space to make 
    // it look good. 
    return height + (10 * 2); 
} 

此代碼是否有任何與它特別不正確的?或者問題可能是其他地方的根源?

謝謝!

回答

1

當您創建的標籤,你把它一個最大高度

MAX(size.height, 44.0f) 

這意味着該標籤是從來沒有超過44分以上。 如果正在打包的文本比標籤的寬度高,那麼它下面的線條仍然在標籤中,但不可見。

您的選擇:

- 製作標籤字體自動調整大小

-change的高度,或者切換到動態高度的標籤

你更新,這將每次去這樣的事情:

myLabel.text = @"some text"; 
CGRect rect = myLabel.frame; 
rect.size.height = textView.contentSize.height;// Adding.size Since height is not a member of CGRect 
textView.frame = rect; //your label is now as high as its contents (the text) 

-trim進入標籤的文本,因此它永遠不會超過標籤的高度。

+0

我想改變高度爲標籤的動態高度可能是最好的在這裏。有沒有簡單的方法來做到這一點? – GeekedOut

+0

這裏有點難,因爲你是從代碼構建你的UI,而不是在可視化編輯器中。 我會更新答案 –

+0

謝謝你的幫助:) – GeekedOut

1

我會擺脫擁有自己的UILabel,除非絕對必要,並根據字符串大小動態調整單元格的高度。看看下面我舉的例子:

裝載的tableview之前創建的字符串

-(void)createStrings{ 

    newArray = [NSMutableArray array]; 

    for(NSDictionary *dictionary in items_array){ 
     NSString *comment = [dictionary objectForKey:(@"comment")]; 
     NSString *first_name = [dictionary objectForKey:(@"first_name")]; 
     NSString *combinedString = [first_name stringByAppendingString:[@": " stringByAppendingString:comment]]; 
     [newArray addObject:combinedString]; 
    } 

} 

#pragma mark - Table View Data Source 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = nil; 

    static NSString *CellIdentifier = @"Cell"; 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    // Configure the cell... 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
     cell.textLabel.numberOfLines = 0; 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17]; 
    } 

    cell.textLabel.text = [newArray objectAtIndex:indexPath.row]; 

    return cell; 
} 

#pragma mark - Table View Delegate 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17]; 
     CGSize constraintSize = CGSizeMake(self.tableView.frame.size.width, MAXFLOAT); 
     CGSize labelSize = [[newArray objectAtIndex:indexPath.row] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

     return labelSize.height + 30; 
}