0

我正在为 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]];
    }

现在,一切正常。细胞生成就像他们期望的那样,并且它按照我设想的方式工作。

问题/问题。

正如您在 currentDataValues 方法中注意到的那样,有一次我正在调用另一种方法来返回风向,它看起来像这样(为了简洁而缩短)。

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

我不得不假设这会导致性能问题,我不明白为什么会这样。

4

4 回答 4

8

每次调用该方法时,都会创建并cellForRowAtIndexPath调用该currentDataValues方法内的字典windDirection

您应该创建一次该字典,例如viewDidLoad并将其存储在表视图控制器的属性或实例变量中。

于 2013-07-10T20:25:13.850 回答
2

-(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-10T20:26:21.413 回答
0

您的方法“currentDataValues”每次被调用时都会创建并返回一个新数组。所以每次调用 tableView:cellForRowAtIndexPath: 时,它都会调用 currentDataValues 并构造并返回一个新数组。

于 2013-07-10T20:28:32.263 回答
0

如果你希望你的方法只被调用一次,你可以做这样的事情。

.h 文件:

@property (nonatomic, strong) NSArray *currentDataValues;   

.m 文件:

{
  if (_currentDataValues == nil) {
    _currentDataValues = [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"]]
  }
  return _currentDataValues;
}

使用 currentDataValues 的此访问器,您的数组将只创建一次。

于 2013-07-11T07:33:34.870 回答