ios - 即使使用 UITableViewCellStyleSubtitle,detailTextLabel 也不会显示

标签 ios objective-c

我试图让字幕显示在我的表格 View 的单元格中,但由于某种原因它没有出现。我将单元格的样式设置为 UITableViewCellStyleSubtitle,所以这不是问题。相关代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NMADRestaurant *restaurant = [self.restaurants objectAtIndex:indexPath.row];
    cell.textLabel.text = [restaurant name];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    CLLocation *restaurantLocation = [restaurant location];
    CLLocationDistance distance = [currentLocation distanceFromLocation:restaurantLocation];
    NSString *distanceString = [NSString stringWithFormat:@"%.1f km",(distance/1000)];
    cell.detailTextLabel.text = distanceString;

    return cell;
}

如果我 NSLog distanceString,它会正确显示。但是当我 NSLog(@"Label %@",cell.detailTextLabel.text) 时,日志中只显示“Label (null)”。

最佳答案

一开始它对我的也不起作用,我必须转到我的 Storyboard,然后选择我的原型(prototype)单元。之后,我转到属性检查器,然后将“样式”从“自定义”更改为详细信息选项之一,然后它显示得很好。

这一行是:

CLLocationDistance distance = [currentLocation distanceFromLocation:restaurantLocation];

将距离声明为 float ?我不确定你得到什么类型的值,所以我不得不做一个

float 距离=0.5;

能够测试它,但也许它是空的,因为你称它为 float ,但它不是?

祝你好运

关于ios - 即使使用 UITableViewCellStyleSubtitle,detailTextLabel 也不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19760457/

相关文章:

iphone - CLLocation - 在另一个位置查找磁偏角/偏差

ios - iOS11中的深度链接回调问题

ios - 加急审核 - 是否重发? iOS应用程序

ios - 在 UIImageView 的右上角添加 UIButton

ios - 通过更新约束在底部的 MGLMapView scaleBar 位置

ios - 动画持续时间 :delay:options:animations:completion: not working (strangely)

ios - 实现带有进度 View 的 iOS 下载管理器的最佳实践

iphone - MonoTouch.Dialog:iPad无法获得背景图像集

iphone - 如何从点数组中绘制填充形状并在该形状内获得点击?

ios - 在 Collection View 之上创建 View ?