ios - 实例变量总是返回零

标签 ios uitableview

我有一个实例变量数组:

       @interface ChannelsVC ()
        {
        NSArray *arrayofFrequencies;
        }
    }
    @end

@implementation ChannelsVC
......

在“viewDidLoad”中进行分配

arrayofFrequencies=[[NSArray alloc]init];

然后,我从 JSON 数据源向数组中插入数据:

-(void)GetFrequencies{

    NSString *urla=@"http://xxxxxxxx";
    NSString *uria = [urla stringByAppendingString:self.lien_satellite];
    NSURL *urlFrequencies= [ NSURL URLWithString:uria];

    NSLog(@"Frequencies URI: %@",uria); //correct URI



    NSURLRequest *request = [NSURLRequest requestWithURL:urlFrequencies];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        arrayofFrequencies=[JSON valueForKeyPath:@"frequency"];

        NSLog(@"Frequencies count: %lu", (unsigned long)[arrayofFrequencies count]);//the count here is correct, I have data in the array

     } failure:nil];

    [operation start];

 }

问题是,当想要创建一个 UITableView,其部分数为“[arrayofFrequencies count];”时,它总是返回“0”!它是一个实例变量,它在“GetFrequencies”函数之外返回零。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"number of sections %ld",(long)[arrayofFrequencies count]); // always returns 0
    return [arrayofFrequencies count];
}

我做错了什么吗?

感谢您的帮助。

控制台输出:

013-06-25 12:58:00.163 Frequencies[840:c07] number of sections 0
2013-06-25 12:58:00.165 Frequencies[840:c07] number of sections 0
2013-06-25 12:58:00.641 Frequencies[840:c07] Frequencies count: 4

最佳答案

之后调用[tableView reloadData]

NSLog(@"Frequencies count: %lu", (unsigned long)[arrayofFrequencies count]);//the count here is correct, I have data in the array

要理解的一点是在 viewDidLoad 中调用 tableView 委托(delegate)。那时数组计数为零。后来数组充满了对象。 reloadData 方法再次调用表的委托(delegate)方法。

关于ios - 实例变量总是返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17296630/

相关文章:

在 uiview 中具有超过 1 个 uitextfield 的 iOS 快速委托(delegate)

iphone - 如何在使用 facebook-ios-sdk 的应用程序中实现聊天

ios - 将 UITableView 导出为 PDF

ios - 使用大型数据库时,如何避免在 UITableView 中使用自定义单元格加载时间过长

iphone - 使用 JSON 问题在 UITableView 中导航缓慢

iphone - ios5 登录屏幕卡住

ios - 使用导航处理 Flutter 中的身份验证状态

ios - UITableViewCell 内自定义 UIView 的 drawRect 的错误行为

ios - 在 uitableview 单元格上保持滑动以删除按钮打开然后返回会导致崩溃

ios - 静态方法的宏日志使用问题