2013-08-06 74 views
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)]; 
     } 

時間戳是正確的,座標是沒有。 在後期模型中有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"}] 
+1

你可以包含你的tableView:cellForRowAtIndexPath:方法嗎? – akhalsa

+0

您是否在configureCell:forRowAtIndexPath中記錄了post.location.coordinate.latitude和post.location.coordinate.longitude以查看它們是否返回正確的值? – rdelmar

+0

@akhalsa我在上面的方法中包含 – grantvansant

回答

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]; 
+0

這仍然會返回(0.00,0.00),這讓我認爲問題可能在其他地方。但我很難過。 post.timestamp是從json對象「created_at」映射而來的,並且完美地工作。上面我包含了位置json映射 - 它沒有正確映射我假設它不返回在.json中顯示的緯度/經度。你注意到代碼的其他部分有什麼嗎? – grantvansant