Давайте сделаем cell.textLabel короче - PullRequest
0 голосов
/ 16 февраля 2012

У меня есть приложение с несколькими таблицами.

В этой таблице есть две метки - textLabel с заголовками и detailTextLabel с датами. Но когда заголовок действительно длинный, он показывается поверх detailTextLabel.

Как я могу решить эту проблему?

приписка
Я попытался переопределить метод 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 ]

1 голос
/ 22 февраля 2012

Сначала я создаю пустую ячейку. Затем добавьте туда UILabel как subView с тегом. И поменяй рамку УИЛабель

- (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 голосов
/ 16 февраля 2012
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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...