简体   繁体   中英

Objective-C: Instance variable for table view data source not being initialized

I'm initializing the data source like so:

@interface SelectWifiViewController() {
    NSArray *exampleNetworks;
}
@end

and:

- (void) viewDidLoad: (BOOL) animated {
    [super viewDidLoad];

    exampleNetworks = [NSArray arrayWithObjects:@"network 1", @"network 2", "network 3", nil];
}

I'm using the data source here:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *tableCellIdentifier = @"wifiNetworkCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableCellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:tableCellIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@, Test", [exampleNetworks objectAtIndex:indexPath.row]];

    return cell;
}

which doesn't work; "(null) Test" is printed on each cell.

Edit:

Here is how I'm navigating to this view controller:

- (void) navAction {
    UIStoryboard *selectWifiView = [UIStoryboard storyboardWithName:@"SelectWifiView" bundle:nil];
    UIViewController *vc = [selectWifiView instantiateViewControllerWithIdentifier:@"SelectWifiView"];
   [self.navigationController pushViewController:vc animated:YES];
}

There is no such method as:

- (void)viewDidLoad:(BOOL) animated { ... }

You meant:

- (void)viewDidLoad { ... }

Your current method is never going to be called, but if you eliminate that animated parameter, it should be.

I would suggest adding a breakpoint and/or NSLog statement whenever you suspect that a particular method is not getting called line you think it should be. That will confirm whether it's called or not, and if not, you can then start to consider various reasons why it might not be getting to that line of code (in this case, because the method signature was incorrect).

Please correct viewDidLoad of iOS without animated as following:

- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM