【问题标题】:Can't access the instance variable无法访问实例变量
【发布时间】:2012-04-30 17:09:53
【问题描述】:

我有一个 UTableView 分组。我想将我的数据分成“类别”组。我有一个实例变量,其中包含我数据库中的所有组。

@interface TableViewController : UITableViewController {

    // Response from server
    NSString* serverResponse;
    NSMutableArray *categories;     // This NSMutableArray contains all category's server    
}

@property (strong) NSString *serverResponse;
@property (strong) NSMutableArray *categories;

我用 requestFinished 方法填充了我的 NSMutableArray(一切正常)

- (void)requestFinished:(ASIHTTPRequest *)request
{  
    NSString *result = request.responseString;

    NSArray *jsonArray = (NSArray*)[result JSONValue];

    int count = [jsonArray count]; 
    int i;
    NSDictionary *jsonDict = [[NSDictionary alloc] init];

    for (i = 0; i < count; i++) {
        jsonDict = [jsonArray objectAtIndex:i];
        NSString *current = [jsonDict objectForKey:@"categoriy"];

        [categories addObject:current];
        NSLog(@"%d", [categories count]) // Shows 4 -> ok !
    }
}

但是当我用这个方法调用它时:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"%d", [categories count])   // Show 0 -> bad !
    return [categories count];
}

它显示在控制台“0”上!

我真的不明白为什么!

也许有人可以帮我解决这个问题?

【问题讨论】:

    标签: ios objective-c uitableview cocoa-touch uikit


    【解决方案1】:

    问题可能是在调用requestFinished: 之前调用了numberOfSectionsInTableView:

    我认为你必须重新加载 tableview。

    尝试将[self.tableView reloadData] 或类似的东西放在requestFinished: 的末尾。

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多