2012-02-16 57 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