提问人:Preethi 提问时间:5/6/2021 更新时间:5/6/2021 访问量:194
如何在目标c的tableview单元格中获取解析后的json数据?
How to get the parsed json data inside the tableview cell in objective c?
问:
在这里,我解析了我的 json 数据并保存在 json 中,我必须插入到设计自定义的 tableviewcell 中......在这里,我发布了json解析的代码和tableview单元格...有人可以帮我解决这个问题吗......
NSString *urlstring =@"https://bsedemo.com/instasocial/api/post/feeds?user_id=402&&access_token=$2y$10$TWX2Ym1gM4NBjGCNzTydzuQTZWPcK2tMVw1eWdWvYDFmO4CsKc1kC&&page_no=1&&feed_type=3&&feed_id=&&member_id=402";
NSURL *url = [NSURL URLWithString:urlstring];
[[NSURLSession.sharedSession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
NSError *err;
NSDictionary * jsondata = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSMutableArray *json =[[NSMutableArray alloc] initWithObjects:[jsondata objectForKey:@"result"],nil];
if(err)
{
NSLog(@"Failed to load json:%@",err);
return;
}
for (int i=0;i<json.count;i++)
{
NSLog(@"json:%@",json[i]);
}
for (NSDictionary *dict in json) {
self->user_image = [dict valueForKey:@"user_image"];
self->username = [dict valueForKey:@"user_name"];
self->created_at =[dict valueForKey:@"created_at"];
self->post_text = [dict valueForKey:@"post_text"];
self->post_comment_count = [dict valueForKey:@"post_comment_count"];
self->post_like_count = [dict valueForKey:@"post_like_count"];
//self->media_img = dict[@"media"][0][@"media_image"];
}
}]resume];
[self->tableview reloadData];
在这里,我发布了我尝试过的 tableview 和 tableviewcell 代码......如果我做错了,请更正代码......提前致谢!!
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;//name.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
TableViewCellVC *cell = [[TableViewCellVC alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
/*TableViewCellVC *cell = (TableViewCellVC *)*/
cell=[tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCellVC" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.profileImageView.image = [UIImage imageNamed:@"girl1.jpg"]; //[UIImage imageNamed:[user_image objectAtIndex:indexPath.row]];
cell.profileImageView.layer.cornerRadius = 23.5;
//cell.profileImageView.backgroundColor = [UIColor blackColor];
//[cell.usernameLabel setFont:[UIFont fontWithName:@"Raleway-Thin" size:15]];
cell.usernameLabel.text =[name objectAtIndex:indexPath.row];//[username objectAtIndex:indexPath.row];
//[cell.lastseenLabel setFont:[UIFont fontWithName:@"Raleway-Thin" size:12]];
cell.lastseenLabel.text = @"Today at 23:52";//[created_at objectAtIndex:indexPath.row];
//[cell.discriptionLabel setFont:[UIFont fontWithName:@"Raleway-Thin" size:10]];
cell.discriptionLabel.text =@"All our dreams can come true, if we have the courage to pursue them. – Walt Disney. All our dreams can come true, if we have the courage to pursue them. – Walt Disney.";//[post_text objectAtIndex:indexPath.row];
cell.postImageView.image = [UIImage imageNamed:@"postpic.jpg"];
//media_img[indexPath.row];
// [cell.likeLabel setFont:[UIFont fontWithName:[NSString fontAwesomeIconStringForEnum:FAHeart] size:16]];
cell.likeLabel.text =[NSString fontAwesomeIconStringForEnum:FAHeart];
//[post_like_count objectAtIndex:indexPath.row];
cell.commentLabel.text =[NSString fontAwesomeIconStringForEnum:FAComment]; //[post_comment_count objectAtIndex:indexPath.row];
cell.shareLabel.text=[NSString fontAwesomeIconStringForEnum:FAShare];
cell.moreLabel.text=[NSString fontAwesomeIconStringForEnum:FADotCircleO];
return cell;
}
-(void)tableView
{
tableview = [[UITableView alloc] initWithFrame:CGRectMake(0,topbar.frame.origin.y+topbar.frame.size.height,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height-150) style:UITableViewStylePlain];
tableview.delegate = self;
tableview.dataSource = self;
//tableview.rowHeight=UITableViewAutomaticDimension;
//tableview.estimatedRowHeight=400;
tableview.separatorColor = [UIColor blackColor];
[tableview registerNib:[UINib nibWithNibName:@"TableViewCellVC" bundle:nil]
forCellReuseIdentifier:@"Cell"];
[mainview addSubview:tableview];
}
答: 暂无答案
评论
result
self->…
reloadData
应该在闭包内调用。获得 URL 请求响应后。self->user_image
self->...