2

テーブル ビューのセルにサブタイトルを表示しようとしていますが、何らかの理由で表示されません。セルのスタイルを 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 の場合、正しく表示されます。しかし、I NSLog(@"Label %@",cell.detailTextLabel.text) の場合、ログに表示されるのは "Label (null)" だけです。

4

2 に答える 2

1

私の場合も最初はうまくいきませんでした。ストーリーボードに移動してから、プロトタイプのセルを選択する必要がありました。その後、属性インスペクターに移動し、「スタイル」をカスタムから詳細オプションの 1 つに変更すると、問題なく表示されました。

そして、次の行を実行します。

CLLocationDistance distance = [currentLocation distanceFromLocation:restaurantLocation];

距離を float として宣言しますか? あなたが得た値のタイプがわからなかったので、

フロート距離 = 0.5;

それをテストできるようにするためですが、おそらく null bc だったので、float と呼んでいましたが、そうではありませんでしたか?

幸運を

于 2013-11-04T04:31:19.623 に答える
1

こんにちは、お知らせ: 私の英語は下手です。

init または viewDidLoad メソッドにこれがありましたか?

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"abc"];

コメントして、もう一度やり直してください。(セルが最初に作成されたとき、このコードのためにスタイルサブタイトルが適用されず、デフォルトのセルスタイルが使用されると思います。)

于 2014-08-18T03:48:46.260 に答える