我的CLLocation有一些内存问题。
CLLocation *annotation = [[CLLocation alloc] initWithLatitude:[[tempDict objectForKey:@"lat"] doubleValue] longitude:[[tempDict objectForKey:@"lon"]doubleValue]];
CLLocation *item2 = [[CLLocation alloc] initWithLatitude:[newLatString doubleValue] longitude:[newLongString doubleValue]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f km",[item2 distanceFromLocation:annotation]/1000];
[annotation release];
[item2 release];所以我试着这样做,但是我意识到你不能设置注解的坐标。
CLLocationCoordinate2D tempCoordinate = annotation.coordinate;
tempCoordinate.latitude = [[tempDict objectForKey:@"lat"] doubleValue];
tempCoordinate.longitude = [[tempDict objectForKey:@"lon"] doubleValue];
annotation.coordinate = tempCoordinate;有解决方法吗?我不想在每次调用cellForRowAtIndexPath时都分配/初始化CLLocation。
发布于 2010-09-28 12:16:14
您的结果对象是一个NSString -只需创建一个包含NSString的类,以及必要时的中间数据的引用/ivars。然后使用观察者习惯用法,只需在字符串发生变化时更新单元格(设计时使字符串依赖于坐标)。您可能会创建一个类,该类在初始化时接受一组参数(例如坐标),在初始化期间创建一个NSString,然后在数据从不更改的情况下引用结果。这真的取决于您期望哪些数据会发生变化,以及以什么频率变化。
发布于 2010-09-28 11:55:45
我不想在每次调用cellForRowAtIndexPath时都分配/初始化CLLocation。
为什么不行?你知道它会导致性能问题吗?您可以立即释放它们,这样它们就不会占用额外的内存。CLLocation看起来像是一个非常轻量级的类,而且Objective-C运行时进行了大量优化,所以它们可能很快就会分配/初始化。在您看到滚动/性能/内存问题之前,我会选择工作正常且易于维护的解决方案。
过早优化是万恶之源-- Donald Knuth
https://stackoverflow.com/questions/3809524
复制相似问题