ios - 将 JSOn 绘制到 UITableviewCell 时出错

标签 ios objective-c json nsarray nsdictionary

我有这样的 json (有字典和数组)。 我想将所有 json 绘制到 UItableviewcell 中,但它不绘制在那里

{   data: [
      {

        featured: true,
        price: {
                    currency: "IDR",
                    amount: 5557679,
                    formatted: "Rp5.558.000"
      },
] }

这是我的 viewcontroller.h 文件

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UITableView *mainTableView;
    NSURL *URL;
    NSDictionary *Deals;
    NSArray *Deals_array;
    NSMutableData *data;
}

@end

这是我的 .m 文件

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"SOme Apps";
    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
    URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://someurl.com"]];
    NSURLRequest *request=[NSURLRequest requestWithURL:URL];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    /*dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: URL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });*/
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    data=[[NSMutableData alloc] init];
    NSLog(@"%@",data);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];

}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSError* error;
    Deals= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    Deals_array = [Deals objectForKey:@"data"]; //2
    NSDictionary* loan = [Deals_array objectAtIndex:0];
    NSString *test=[loan objectForKey:@"featured"];
    NSLog(@"%@",test);
    [mainTableView reloadData];




}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"Error");
}

////set minimum section oftableview
-(int)numberOfTableviewSection : (UITableView *) tableView
{
    return 1;
}

////set length uitableview
-(int) tableView :(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [Deals_array count];
}

///declaring uitableview
-(UITableViewCell *)tableView :(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell==nil){
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }
    cell.textLabel.text=[[Deals_array objectAtIndex:indexPath.row] objectForKey:@"featured"];
    return  cell;
}
 @end

但它没有绘制对象键@test,有人可以帮助我为什么吗?

最佳答案

尝试进行分配

Deals_array = [[NSArray alloc] initWithArray:[Deals objectForKey:@"data"]];

关于ios - 将 JSOn 绘制到 UITableviewCell 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12811683/

相关文章:

ios - 如何让 UIView 在滚动时改变它的大小

ios - UISegmentedControl 粘滞模式

objective-c - 如何使用 VideoToolbox 将音频和视频一起编码为 h264 格式?

python - 多线程环境和模块,如 pickle 或 json

c# - 保护客户端json

javascript - Json 字符串结果用 JavaScript 计数

ios - View Controller 不会关闭并立即呈现新的 View Controller

ios - 我如何为 iOS 设备生成和使用唯一的 deviceId?

objective-c - 将 Objective-c 框架导入 Swift 框架(Google Analytics + Cocoapod)

ios - 如何在 iOS 中实现交互式 PDF