2016-04-16 6 views
0

ViewControllerを持っていますが、TableViewを配置した中央に別の小さなUIViewを配置していますが、このテーブルの配列からデータを表示する方法がわからないと、感謝。私はこのコードを使用してテーブルをロードしようとしている:私はNSMutableArrayのをロードするために、私のテーブルビューにカスタムセルを使用していますUIViewで読み込むテーブルを取得できません

-(void) animateResults { 


_resultsLabel.text = [NSString stringWithFormat:@"You Scored %d", runningScore ]; 
resultsTable.delegate = self; 
resultsTable.dataSource = self; 
[self.resultsTable registerClass:[resultsViewCell self] forCellReuseIdentifier:@"resultsListCell"]; 
[self.resultsTable reloadData]; 
[self.view layoutIfNeeded]; 
[UIView animateWithDuration:0.3 animations:^{ 
    _resultsView.frame = self.view.frame; 


} completion:^(BOOL finished){ 

    NSLog(@"%@", questionsArray); 

}]; 


} 

。私は試しましたテーブルビューのこのコードを使用して:

-(NSInteger)numberOfSectionsInTableView:(resultsViewCell *)tableView { 
//Return number of sections 
return 1; 

} 

//get number of rows by counting number of challenges 
-(NSInteger)tableView:(resultsViewCell *)tableView numberOfRowsInSection:(NSInteger)section { 
return questionsArray.count; 
} 


//setup cells in tableView 
-(resultsViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
//setup cell 
static NSString *CellIdentifier = @"resultsListCell"; 

resultsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

NSDictionary *results = [questionsArray objectAtIndex:indexPath.row]; 

NSString *resultsName = [results objectForKey:@"answers"]; 

BOOL correct = [[results objectForKey:@"correct"] boolValue]; 

if (!correct) { 
    cell.resultsIcon.image = [UIImage imageNamed:@"BlackIconLock.png"]; 
} 

else{ 
    cell.resultsIcon.image = nil; 
} 

cell.resultsName.text = resultsName; 

return cell; 
} 

しかし、_resultsViewはなぜロードしないのですか?私はこれまでに多くの大きな助けを得ており、本当に感謝しています。私はこの時点で約2週間立ち往生してきた。助けてください!

+0

'_resultView'が' nil'であることを確認してください。 – dasdom

+0

申し訳ありませんが、私はこの学習の初心者です。どうすれば確認できますか?もう一度noobnessのために申し訳ありません – JoeShmoe

+0

'NSLog(@" resultsView:%@ "、_resultsView)'行を 'animateResults'の先頭に追加し、アプリを実行してコンソール出力を見てください。 'resultsView :(null)'を出力すると、ビューはnilになります。 – dasdom

答えて

0

I FINALLY!私の意図はUIViewの内部にUITableViewのロードを持つことでした。私は、ViewController上にTableViewデリゲートとデータソースをドラッグ&ドロップしようとしましたが、アプリがクラッシュしたときにロードされました。だから、テーブルビューのデリゲートとデータソースをUIViewのロードメソッドに移しました。このメソッドを使って試しました。

-(void) animateResults { 
//this part of my method actually worked it displayed the final score from a predifined variable. 
_resultsLabel.text = [NSString stringWithFormat:@"You Scored %d", runningScore ]; 
//i tried using this to set my UITableView delegate & dataSource when the method was initiated instead of UIViewController load. 
resultsTable.delegate = self; 
resultsTable.dataSource = self; 
//used this code to register UIViewCell that was nonexistent, 
//i declared *CellIdentifier = @"resultsListCell" and it should have 
//been *CellIdentifier = @"resultsCell". so i received an error used this code to solve it. 
//duh. cell don't exist must not be the cell you created.My Bad. 
[self.resultsTable registerClass:[resultsViewCell self] forCellReuseIdentifier:@"resultsListCell"]; 
//this code worked but after debugging i found out there was no data to load. 
[self.resultsTable reloadData]; 
//was desperate at this point not sure why i put this in. 
[self.view layoutIfNeeded]; 
//this also worked loads my UIView. 
[UIView animateWithDuration:0.3 animations:^{ 
_resultsView.frame = self.view.frame; 

} completion:^(BOOL finished){ 

NSLog(@"%@", questionsArray); 

}]; 

} 

私の解決策は簡単です。データソースをドラッグアンドドロップしようとしたときに私のアプリがクラッシュした理由は、「静的NSString * CellIdentifier」のラベルが間違っていたためでした。だから、あなたのセル識別子が正しいことを確認してください!私はUITableViewのdataSourceを読み込み、上記のメソッドでデリゲートしようとしました。私はその後これに上記の方法を変更しました。

-(void) animateResults { 

//populate label 
_resultsLabel.text = [NSString stringWithFormat:@"You Scored %d", runningScore ]; 

//reload table 
[self.resultsTable reloadData]; 

//Load View 
[UIView animateWithDuration:0.3 animations:^{ 

    _resultsView.frame = self.view.frame; 

} completion:^(BOOL finished){ 

    NSLog(@"%@", questionsArray); 

}]; 
} 

このトリミングコードは、データソースを接続してUIViewControllerにデリゲートしたために機能します。これにより、UIViewControllerがロードされたときにテーブルがロードされるので、プログラムでそのようにする必要はありません。また、誤ラベルセル識別子を修正することで、registerClass変数の必要性がなくなりました。お役に立てれば。

0

あなたはXcodeの7の上に使用している場合は、ストーリーボードでのViewControllerに真ん中をUIContainerView使用するcontainerViewへの自動埋め込みのUITableView、例completion blockanimateResultsにデータをリロードするuse this way its embed and load array will be showed

0

してみてください。このようなもの

completion:^(BOOL finished){ 

    NSLog(@"%@", questionsArray); 
    [self.resultsTable reloadData]; 

}]; 
+0

私はそれを行い、このエラーを受け取りました - [UITableViewCell resultsIcon]:認識できないセレクタがインスタンス – JoeShmoe

+0

に送信されました。あなたはinterface builderからtableviewクラスを設定していますか?はいの場合、なぜanimateResultメソッドのクラスを登録していますか? – Lion

+0

これを削除する[self.resultsTable registerClass:[resultsViewCell self] forCellReuseIdentifier:@ "resultsListCell"] ;?私がそれをしたら、私はUnknown class resultsをInterface Builderファイルで取得します。私のデバッグの始めに。また、UIView tableView:numberOfRowsInSection:]:インスタンスに送信された認識できないセレクタを受け取る – JoeShmoe

関連する問題