我有一个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"}]
答案 0 :(得分: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];