首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方法被调用9次,应调用一次

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

Stack Overflow用户
提问于 2013-07-11 04:21:03
回答 4查看 109关注 0票数 0

我正在为iOS编写一个表视图应用程序,天气是我正在实现的东西。

在我的第一部分(第0部分)中,我有9行(0-8)的基本数据。

代码语言:javascript
复制
-(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中,我有以下内容。

代码语言:javascript
复制
- (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,它看起来像这样(为了简洁起见)。

代码语言:javascript
复制
-(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条。

代码语言:javascript
复制
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

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-07-11 04:26:21

对于tableview试图显示的每一项,都会调用-(NSArray *)currentDataValues方法(如果我理解正确的话,是9项)。在Objective-C中,“点”语法(比如你出现的self.currentValuesDict)实际上只是调用同名方法的一个别名。它不会在幕后执行任何缓存或花哨的行为。所以身体:

代码语言:javascript
复制
{
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时生成并缓存此数组。

票数 2
EN

Stack Overflow用户

发布于 2013-07-11 04:25:14

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

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

票数 8
EN

Stack Overflow用户

发布于 2013-07-11 04:28:32

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17580178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档