调用方法9次,应调用一次

Pet*_*oti 0 cocoa-touch objective-c

我正在为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"]];
}
Run Code Online (Sandbox Code Playgroud)

然后在我的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]];
    }
Run Code Online (Sandbox Code Playgroud)

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

问题/问题.

正如你在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";
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经在那里进行测试,这就是问题所在.当调用该方法时,我只得到1时会收到9条日志消息.

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
Run Code Online (Sandbox Code Playgroud)

我必须假设这会导致性能问题,我不明白为什么会发生这种情况.

Mar*_*n R 8

每次cellForRowAtIndexPath调用的currentDataValues方法,该方法里面的字典创建,因此windDirection调用.

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