ios - 让我们缩短 cell.textLabel 的长度

标签 ios uitableview uilabel

我有一个带有一些表格的应用程序。

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

但是没用

最佳答案

首先我创建空单元格。然后添加 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;
}

关于ios - 让我们缩短 cell.textLabel 的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9307046/

相关文章:

ios - 获取 native 应用程序 objective-c 中当前播放的非音乐应用程序轨道的信息

ios - ReactiveCocoa 3 和 ReactiveCocoa 4 中的多播

java - QR码的黑白主题是标准的吗?

json - Swift 控制台输出到文本标签

ios - 制作 iPhone 应用程序 Universal Xcode 7 时一切都会移动

ios - iOS 7 和 iOS 8 中的动态单元格高度 - 我只能在 iOS 7 中支持 heightForRowAtIndexPath 吗?

iphone - 动态向 UITableViewCell 添加边框 subview

iphone - 在 IOS 6 中设置 UITableView 的背景颜色

IOS 向从 parse.com 检索到的数据插入换行符

iphone - 在 Xcode 上的图像上添加文本