【问题标题】:Detail Text Label Not displaying proper content详细文本标签未显示正确的内容
【发布时间】:2013-08-06 17:58:17
【问题描述】:

我有一个加载帖子供​​稿的 iOS 应用。表格单元格文本标签显示 post.content。我试图让 detailTextLabel 显示 post.location 和时间戳。时间戳有效,但位置返回坐标 (0.000, 0.000)。 这是代码

    - (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        Post *post = [self.posts objectAtIndex:indexPath.row];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.text = post.content;
    cell.detailTextLabel.textColor=[UIColor lightGrayColor];
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:9];

        cell.detailTextLabel.text = [NSString stringWithFormat:@"posted on %@ at (%f, %f)", post.timestamp,  (post.location.coordinate.latitude, post.location.coordinate.longitude)];
       }

时间戳正确,坐标不正确。 在 post 模型中有 CLLocation 属性。 我声明:

static NSString * NSStringFromCoordinate(CLLocationCoordinate2D coordinate) {
    return [ NSString stringWithFormat:@"(%f, %f)", coordinate.latitude, coordinate.longitude];
}

我会像这样获取附近的帖子:

+ (void)fetchNearbyPosts:(CLLocation *)location
          withBlock:(void (^)(NSArray *posts, NSError *error))completionBlock
{
    NSDictionary *parameters = @{
                                 @"lat": @(location.coordinate.latitude),
                                 @"lng": @(location.coordinate.longitude)
                                 };

我使用这个位置字典从 JSON 更新

NSDictionary *locationDictionary = [dictionary objectForKey:@"location"];
self.location = [[CLLocation alloc] initWithLatitude:[[locationDictionary valueForKey:@"lat"] doubleValue] longitude:[[locationDictionary valueForKey:@"lng"] doubleValue]];

在所有这些之间 - 是什么阻止了 detailTextLabel 显示正确的坐标?我认为这是 detailTextLabel 代码中的内容 - 我没有调用正确的坐标名称,因此它返回零 - 事实上,如果我删除整个 post.location.coordinate 部分,标签返回完全相同的内容。我尝试了几种不同的方式来表达位置,但大多数替代方法都会引发异常,这应该有效,不是吗? 有什么想法吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    [self configureCell:cell forRowAtIndexPath:indexPath];
    return cell;
}

这是真正的 JSOn - 以防你在我的映射中看到任何奇怪的东西..

[{"content":"Test","postid":1,"lat":"34.13327300486596","lng":"-118.1054221597022", "created_at":"2013-08-06T14:59:42Z"}]

【问题讨论】:

  • 你能包含你的 tableView:cellForRowAtIndexPath: 方法吗?
  • 您是否在 configureCell:forRowAtIndexPath: 中记录了 post.location.coordinate.latitude 和 post.location.coordinate.longitude 以查看它们是否返回正确的值?
  • @akhalsa 我包含了上面的方法
  • @rdelmar 在日志中返回 0.00, 0.00 - 所以我一定不是在调用正确的东西。根据字典和 json map 你会怎么称呼它?我会在上面添加json
  • 逆向工作,直到找到问题所在。尝试记录 self.location 和 locationDictionary。他们中的任何一个都给出了正确的值吗?

标签: ios xcode uitableview core-location


【解决方案1】:
cell.detailTextLabel.text = [NSString stringWithFormat:@"posted on %@ at (%f, %f)",
    post.timestamp,  
    (post.location.coordinate.latitude, post.location.coordinate.longitude)];

去掉纬度和经度周围的那些多余的括号:

cell.detailTextLabel.text = [NSString stringWithFormat:@"posted on %@ at (%f, %f)",
    post.timestamp, 
    post.location.coordinate.latitude, 
    post.location.coordinate.longitude];

【讨论】:

  • 这仍然返回 (0.00, 0.00) 这让我认为问题可能出在其他地方。但我很难过。 post.timestamp 是从 json 对象“created_at”映射而来的,效果很好。上面我包含了位置 json 地图 - 我假设它没有正确映射,因为它没有返回 .json 中显示的 lat/lng 你注意到代码的其他部分有什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多