让我们让cell.textLabel更短

时间:2012-02-16 07:17:55

标签: ios uitableview uilabel

我有一些带有桌子的应用程序。

在此表中有两个标签 - 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;
}

但它不起作用

2 个答案:

答案 0 :(得分:1)

首先我创建空单元格。然后将UILabel添加为带有标记的subView。并改变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;
}

答案 1 :(得分: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);