これは私のシナリオです。ViewController1とClass1(サービスクラス)があります。
ViewController1でデリゲートとデータソースを設定して、nibにtableViewをロードしています。viewDidLoadで、別のクラス(Class1)のnetworkCall関数を呼び出しています。Class1では、応答を取得した後、応答データの配列をViewController1の関数に渡します。ここで、データはテーブルビューに入力されます。
xibでデータソースとデリゲートを接続しました。問題:ViewController1で配列として応答を取得すると、UITableViewがnilになり、reloadDataを使用できませんが、配列にサーバーからのアイテムのリストが含まれています。
これが私のコードです
ViewController1
- (void)viewDidLoad
{
[super viewDidLoad];
ClassA *class = [[ClassA alloc]init];
[class getResponse];
}
//This method is calling from ClassA using delegate
-(void)responseData:(NSArray*)arrayList
{
//arrayList have response data
[tableView reloadData];//here tableView becomes nil.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"array count %d",array.count);//has number of items(for me, its 3).
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableView";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"dsds";
return cell;
}
tableViewが初めて呼び出しています。
インターフェイスのViewController1で、プロトコルを設定しています
<UITableViewDelegate,UITableViewDataSource>