如何在目标c的tableview单元格中获取解析后的json数据?

How to get the parsed json data inside the tableview cell in objective c?

提问人:Preethi 提问时间:5/6/2021 更新时间:5/6/2021 访问量:194

问:

在这里,我解析了我的 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];
}
iOS JSON Objective-C Xcode 表视图

评论

0赞 vadian 5/6/2021
至少在主线程的块内重新加载表。你要完成什么?key 的值是一个数组,但在循环中,您将覆盖每次迭代中的属性。我建议为 .model 创建一个自定义类。resultself->…
0赞 Preethi 5/6/2021
@vadian自>user_image是一个数组,下面的对象也是可变的数组对象。我需要完成“解析json数据并将其放入tableview单元格中”。你能帮帮我吗..
0赞 Larme 5/6/2021
reloadData应该在闭包内调用。获得 URL 请求响应后。
0赞 vadian 5/6/2021
如果是一个数组,则没有区别。它将包含一个对象,即循环最后一次迭代的数据。属性是如何声明的?self->user_imageself->...

答: 暂无答案