2013-08-02 63 views
0

我正在构建一个iphone应用程序,显示表视图中的帖子。每篇文章都标有用户当前位置,我正努力在详细文本标签中显示。 post模型包括这些属性显示详细位置TextLabel

@property (nonatomic, strong) NSString *content; 
@property (strong) CLLocation *location; 

在索引视图我将单元配置像这样:

- (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; 
} 

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

    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 
    cell.textLabel.text = post.content; 

这正确返回后的内容。 但是,当我尝试在字幕中包含lat/lng时,它会崩溃。 这会导致系统崩溃,并引发了一个不兼容的指针类型的异常“的NSString从CLLocation”:

cell.detailTextLabel.text = post.location; 

这是有道理的,因为的.text期待一个字符串,位置在字典中,像这样初始化:

- (id)initWithDictionary:(NSDictionary *)dictionary { 
    self = [super init]; 
    if (!self) { 
     return nil; 
    } 

    self.content = [dictionary valueForKey:@"content"]; 
    self.location = [[CLLocation alloc] initWithLatitude:[[dictionary nonNullValueForKeyPath:@"lat"] doubleValue] longitude:[[dictionary nonNullValueForKeyPath:@"lng"] doubleValue]]; 

    return self; 
} 

那么如何返回字幕标签中的位置? 我也想显示一个时间戳,并怀疑它是一个类似的解决方案。 在我的岗位模型实现文件我#IMPORT“ISO8601DateFormatter.h”以格式字符串的日期,同样我:

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

但我不知道如何将所有成一个简单的detailTextLabel这一点。

任何帮助将不胜感激。

编辑

我做了这个数额的进步: 的纬度和经度显示integers-但它不是正确的纬度/经度,即它实际上没有读取正确的整数。

cell.detailTextLabel.text = [NSString stringWithFormat:@"at (%f, %f)", post.location]; 

lat和显示LNG是这样的: 0.00,-1.9 当它应该是: LAT “:” 37.785834" , “LNG”:“ - 122.406417。 所以它实际上并没有读到“post.location” 这行的结尾,那我怎么才能让它显示正确的数据呢?

回答

0

你有没有试过这个。

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

     cell.detailTextLabel.text = NSStringFromCoordinate(post.location); 
     //.....more setup code 
    } 
+0

干杯埃斯克,纠正。 –

+0

引发异常:ARC不允许将int隐式转换为NSString。 – grantvansant

+0

你正在试图使用一个字符串,它期望一个int。它发生在哪条线上? –