0
私はサーバからデータを取得していて、それをtableview
に表示できません... jsonarray
という変数にデータを格納していますが、jsonarray.count
をゼロ以外の完了ハンドラとして表示しています。 (行の番号はnumberOfRowsInSection
)。テーブルビューにデータを表示できません
@interface Staffdir()
@property(nonatomic,strong) NSMutableArray* jsonarr;
@end
@implementation Staffdir
- (void)viewDidLoad {
_namesarr=[[NSMutableArray alloc]init];
_occupationarr=[[NSMutableArray alloc]init];
_Phonearr=[[NSMutableArray alloc]init];
_faxnumarr=[[NSMutableArray alloc]init];
NSMutableURLRequest*req=[[NSMutableURLRequest alloc]init];
NSURL*url=[NSURL URLWithString:@"http:someurl"];
[req setURL:url];
[[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
self.jsonarr = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
NSDictionary *dict=[[NSDictionary alloc]init];
for(int i=0;i<self.jsonarr.count;i++){
dict=[self.jsonarr objectAtIndex:i];
[_namesarr addObject:[dict objectForKey:@"Name"]];
[_faxnumarr addObject:[dict objectForKey:@"Fax_Number"]];
[_Phonearr addObject:[dict objectForKey:@"Phone"]];
[_occupationarr addObject:[dict objectForKey:@"Job_Title"]];
}
}] resume];
[self.tableview reloadData];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.jsonarr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Staffcustom *cell= [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[Staffcustom alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.namelabel.text=[_namesarr objectAtIndex:indexPath.row];
cell.namelabel.textColor = [UIColor blueColor];
cell.occupationlabel.textColor=[UIColor colorWithRed:0.5 green:0.0 blue:1 alpha:1.0];
cell.occupationlabel.text=[_occupationarr objectAtIndex:indexPath.row];
[email protected]"12345678";
[email protected]"12345678";
UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-1,cell.frame.size.width,0.1)];
additionalSeparator.backgroundColor = [UIColor blueColor];
[cell addSubview:additionalSeparator];
return cell;
}
- (IBAction)Btn_Back:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
'dataTaskWithRequest'はすでにasycnです。その上でディスパッチを呼び出す必要はありません。補完ハンドラの中で '[self.tableview reloadData];を呼び出す必要があります。 – Larme
要求を返すデータに値がありますか?はいの場合は、jsonの応答の構造を見せてください。 – DiegoQ
これを試してくださいNSDictionary * response = [[NSDictionary alloc] init]; レスポンス= [[NSMutableArray alloc] init]; for(int i = 0; i <[responseDict valueForKey:@ "dictonaryName"] count]; i ++){ [レスポンスaddObject:[[responseDict valueForKey:@ "name"] objectAtIndex:i]]; } [self.tableView reloadData]; –