Ios 在DetailTextLabel中显示位置
我正在构建一个iphone应用程序,在表视图中显示帖子。每篇文章都带有用户当前位置的标签,我很难将其显示在详细信息文本标签中。 post模型包含这些属性Ios 在DetailTextLabel中显示位置,ios,location,tableview,Ios,Location,Tableview,我正在构建一个iphone应用程序,在表视图中显示帖子。每篇文章都带有用户当前位置的标签,我很难将其显示在详细信息文本标签中。 post模型包含这些属性 @property (nonatomic, strong) NSString *content; @property (strong) CLLocation *location; 在索引视图中,我对单元格进行如下配置: - (UITableViewCell *)tableView:(UITableView *)tableView cellFo
@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 from CLLocation”:
这是有意义的,因为.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;
}
那么如何返回字幕标签中的位置呢?
我还想显示一个时间戳,并怀疑这是一个类似的解决方案。
在我的后期模型实现文件中,我#导入“ISO8601DateFormatter.h”以从日期格式化字符串,类似地,我有:
static NSString * NSStringFromCoordinate(CLLocationCoordinate2D coordinate) {
return [ NSString stringWithFormat:@"(%f, %f)", coordinate.latitude, coordinate.longitude];
}
但我不知道如何将这一切绑定到一个简单的detailTextLabel中
任何帮助都将不胜感激
编辑
我取得了这么大的进步:
lat和lng显示整数-但它不是正确的lat/lng,即它实际上没有读取正确的整数
cell.detailTextLabel.text = [NSString stringWithFormat:@"at (%f, %f)", post.location];
显示的lat和lng如下所示:
0.00, -1.9
何时应该:
lat:“37.785834”,“lng:”-122.406417。
因此,实际上并不是阅读“post.location”这行的末尾
那么我如何才能让它显示正确的数据呢?您尝试过这个吗
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
Post *post = [self.posts objectAtIndex:indexPath.row];
cell.detailTextLabel.text = NSStringFromCoordinate(post.location);
//.....more setup code
}
这引发了一个异常:ARC不允许将int隐式转换为NSString。您试图在需要int的位置使用字符串。它发生在哪行?我在[NSString stringWithFormat:@“at(%f,%f)”、post.location.coordinate.latitude、post.location.coordinate.longitude]上添加了一些进度;这将为您提供位置坐标的单个双精度值
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
Post *post = [self.posts objectAtIndex:indexPath.row];
cell.detailTextLabel.text = NSStringFromCoordinate(post.location);
//.....more setup code
}