0

我有一个加载帖子提要的 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"}]
4

1 回答 1

0
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];
于 2013-08-07T13:16:15.077 回答