【发布时间】: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