【问题标题】:Displaying detailTextLabel in non-custom prototype UITableViewCell whilst using Storyboard使用 Storyboard 时在非自定义原型 UITableViewCell 中显示 detailTextLabel
【发布时间】:2012-03-17 17:19:33
【问题描述】:

我希望这些足以解决我的问题:

不幸的是,要么我太笨,看不到我在这些 cmets 中的解决方案,要么他们没有解决我的问题。

我很固执,因为我相信我应该能够在不创建自定义 UITableViewCell 的情况下显示详细文本标签,同时通过情节提要创建我的界面。默认情节提要原型单元格没有样式选项。同样,在我的代码中,除了这里之外,我实际上没有在哪里分配这个单元格;因此,我假设它最初在情节提要中分配给不使用详细文本标签的默认样式。

当我使用以下代码填充单元格时,我的 detailTextLabel 没有出现:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSLog(@"This is never called!");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    // Configure the cell...
    switch(indexPath.section) {
        case 0: // boring cell
            cell.textLabel.text = [NSString stringWithFormat:@"Default label"];
            cell.textLabel.textAlignment = UITextAlignmentCenter;
            break;

        case 1: // loading cells based on contents of core data
            {           
            // get some data from previously retrieved core data objects
            NSManagedObject *rowObj = [myObjects objectAtIndex:[indexPath row]];
            NSDate *someDate = (NSDate*)[rowObj valueForKey:@"timeStarted"];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setTimeStyle:NSDateFormatterNoStyle];
            [formatter setDateStyle:NSDateFormatterMediumStyle];
            NSString *stringFromDate = [formatter stringFromDate: someDate];

            // we have the fields ready; populate the cell
            NSLog(@"This prints a valid date:%@", stringFromDate);

            // this, also, displays just fine
            cell.textLabel.text = [NSString stringWithFormat:@"Default text"];

            // this does not appear
            cell.detailTextLabel.text = stringFromDate;
    break;
        }           
        default:
            break;
    }
    return cell;
}

如果这个问题已经在其他地方得到解决并且我的搜索功能缺失,或者如果有关于 UITableViewCells 和与详细文本标签单元格样式相关的故事板的讨论,我提前道歉,但我在搜索中没有看到它们。

具体来说,我需要在我的代码中进行哪些更改以设置样式以使用详细文本标签,同时仍使用非自定义原型 UITableViewCells 并保持我的故事板完整?

最后,这可能完全不相关(如果是,请忽略),如果我生成许多行以便我可以向下滚动表格,我的所有行看起来都很好,除了有几个(取决于我有多快滚动)看起来与其余部分不同(文本对齐)。也就是说,据我了解,我看到新单元被分配/初始化而没有我在情节提要中指定的相同默认设置。请注意,下面的 initWithStyle 代码仍然从未被调用。如果我正确理解这一点,那么幕后正在创建新单元格,我想知道在哪里可以设置样式偏好。但同样,如果这是不相关问题的症状,请忽略,如果它仍然存在,我可以发布另一个问题。

对不起,文字墙。

【问题讨论】:

    标签: ios5 uitableview storyboard


    【解决方案1】:

    我找到了解决首要问题的方法;我相信我问题的最后一段是一个不相关的问题。

    使用情节提要时,请勿将标识符设置为与代码中使用的标识符相同;您不会使用故事板中使用的默认单元格类型。通过共享相同的标签,情节提要的已识别单元格的属性优先,看起来像。

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 2016-02-03
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多