我正在为iOS编写一个表视图应用程序,天气是我正在实现的东西。
在我的第一部分(第0部分)中,我有9行(0-8)的基本数据。
-(NSArray *)currentDataValues
{
return @[
[self dataValueForKey:@"cloudCover"],
[self dataValueForKey:@"dewPoint"],
[self dataValueForKey:@"humidity"],
[self dataValueForKey:@"precipIntensity"],
[self dataValueForKey:@"precipProbability"],
[self dataValueForKey:@"temperature"],
[self dataValueForKey:@"visibility"],
[self windDirection:[self.currentValuesDict valueForKey:@"windBearing"]],
[self dataValueForKey:@"windSpeed"]];
}然后,在我的cellForRowAtIndexPath中,我有以下内容。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier;
if (indexPath.section == 0) {
cellIdentifier = @"Summary Cell";
} else {
cellIdentifier = @"Minutely Cell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
if([indexPath section] == 0){
cell.textLabel.text = [self.currentDataPoints objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [self.currentDataValues objectAtIndex:[indexPath row]];
}现在,一切都正常了。这些单元格生成的id与它们所期望的一样,并且它的工作方式与我预想的一样。
问题/问题。
正如你在currentDataValues方法中注意到的,我调用了另一个方法来返回Wind Direction,它看起来像这样(为了简洁起见)。
-(NSString *)windDirection:(NSNumber *)value
{
NSLog(@"%@", value);
if (value.floatValue > 11.25 && value.floatValue < 33.75) {
return @"North North East";
} else if (value.floatValue > 33.75 && value.floatValue < 56.24) {
return @"North East";
}
}我把日志放在那里进行测试,这就是问题所在。当该方法被调用时,我得到9条日志消息,而我应该只得到1条。
2013-07-10 16:19:13.502 XX Weather[48831:c07] 260
2013-07-10 16:19:13.503 XX Weather[48831:c07] 260
2013-07-10 16:19:13.503 XX Weather[48831:c07] 260
2013-07-10 16:19:13.503 XX Weather[48831:c07] 260
2013-07-10 16:19:13.504 XX Weather[48831:c07] 260
2013-07-10 16:19:13.504 XX Weather[48831:c07] 260
2013-07-10 16:19:13.504 XX Weather[48831:c07] 260
2013-07-10 16:19:13.505 XX Weather[48831:c07] 260
2013-07-10 16:19:13.505 XX Weather[48831:c07] 260我不得不假设这会导致性能问题,我不明白为什么会发生这种情况。
发布于 2013-07-11 04:26:21
对于tableview试图显示的每一项,都会调用-(NSArray *)currentDataValues方法(如果我理解正确的话,是9项)。在Objective-C中,“点”语法(比如你出现的self.currentValuesDict)实际上只是调用同名方法的一个别名。它不会在幕后执行任何缓存或花哨的行为。所以身体:
{
return @[
[self dataValueForKey:@"cloudCover"],
[self dataValueForKey:@"dewPoint"],
[self dataValueForKey:@"humidity"],
[self dataValueForKey:@"precipIntensity"],
[self dataValueForKey:@"precipProbability"],
[self dataValueForKey:@"temperature"],
[self dataValueForKey:@"visibility"],
[self windDirection:[self.currentValuesDict valueForKey:@"windBearing"]],
[self dataValueForKey:@"windSpeed"]];
}对这9次求值,包括调用windDirection的那一行。您需要在表视图控制器的初始化中或在第一次调用currentDataValues时生成并缓存此数组。
发布于 2013-07-11 04:25:14
每次cellForRowAtIndexPath调用currentDataValues方法时,都会创建该方法内部的字典,因此会调用windDirection。
您应该创建该字典一次,例如在viewDidLoad中,并将其存储在表视图控制器的属性或实例变量中。
发布于 2013-07-11 04:28:32
您的方法"currentDataValues“每次被调用时都会创建并返回一个新的数组。因此,每次调用tableView:cellForRowAtIndexPath:时,它都会调用currentDataValues,构造并返回一个新的数组。
https://stackoverflow.com/questions/17580178
复制相似问题