[英]Objective-C: Instance variable for table view data source not being initialized
我正在初始化數據源,如下所示:
@interface SelectWifiViewController() {
NSArray *exampleNetworks;
}
@end
和:
- (void) viewDidLoad: (BOOL) animated {
[super viewDidLoad];
exampleNetworks = [NSArray arrayWithObjects:@"network 1", @"network 2", "network 3", nil];
}
我在這里使用數據源:
- (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;
}
這不起作用; 在每個單元格上打印“(null)測試”。
編輯:
這是我如何導航到這個視圖控制器:
- (void) navAction {
UIStoryboard *selectWifiView = [UIStoryboard storyboardWithName:@"SelectWifiView" bundle:nil];
UIViewController *vc = [selectWifiView instantiateViewControllerWithIdentifier:@"SelectWifiView"];
[self.navigationController pushViewController:vc animated:YES];
}
沒有這樣的方法:
- (void)viewDidLoad:(BOOL) animated { ... }
你的意思是:
- (void)viewDidLoad { ... }
您的當前方法永遠不會被調用,但是如果您消除了該animated
參數,它應該是。
每當你懷疑某個特定方法沒有得到你認為它應該被調用的行時,我建議添加一個斷點和/或NSLog
語句。 這將確認是否被調用,如果沒有,您可以開始考慮可能無法訪問該行代碼的各種原因(在這種情況下,因為方法簽名不正確)。
請更正iOS的viewDidLoad,不要動畫如下:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.