2012-02-16 59 views
0

我得到了一些應用程序的表。讓我們使cell.textLabel更短

在這個表格中有兩個標籤 - 帶標題的textLabel和帶日期的detailTextLabel。但是當標題很長時,它顯示在detailTextLabel上。

我該如何解決這個問題?

P.S.
我試圖重寫這樣layoutSubviews方法:

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    CGSize size = self.bounds.size; 
    CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height); 
    self.textLabel.frame = frame; 
    self.textLabel.contentMode = UIViewContentModeScaleAspectFit; 
} 

但它不工作

+1

你試試看cell.textLabel.numberOfRows = 1; – Hiren 2012-02-16 07:22:01

+0

如果您的意思是numberOfLines - 它不工作。 – 2012-02-16 07:28:55

回答

1

首先,我創建空單元格。然後在那裏添加UILabel作爲帶標籤的子視圖。並更改UILabel的框架

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Recent Dream"; 

    UITableViewCell *cell;// = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     NSLog(@"new cell"); 

     UILabel *textLabel = [[UILabel alloc] init]; 
     textLabel.frame = CGRectMake(10, 0, self.view.frame.size.width -110, 48); 
     textLabel.backgroundColor = [UIColor clearColor]; 
     textLabel.font = [UIFont boldSystemFontOfSize:16]; 
     [textLabel setTag:1]; 
     [cell.contentView addSubview:textLabel]; 
    } 

    // Configure the cell... 

    Dream *tempDream = [self.dreams objectAtIndex:indexPath.row]; 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MM/dd/yyyy"]; 

    //Optionally for time zone converstions 
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; 

    NSString *stringFromDate = [formatter stringFromDate:tempDream.dateAdd]; 

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bodyWrapper.png"]]; 

    cell.contentView.backgroundColor = background; 

    UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:1]; 

    textLabel.text = tempDream.title; 

    cell.textLabel.text = @""; 
    cell.detailTextLabel.text = stringFromDate; 

    return cell; 
} 
0
float newWidth = 30; //to try increase the value, while all will be right. 
CGRect frame = CGRectMake(4.0f, 4.0f, size.width - newWidth, size.height); 
+0

之前,我試圖將size.width替換爲數字。它不是解決問題=( – 2012-02-16 10:51:29