来自JSON的动态内容仅发布/重复最后一个密钥的数据(应该打印所有值)

时间:2014-03-28 03:49:30

标签: php ios objective-c json uitableview

我有一个UITableView(和UITableVIewCell),我正在通过JSON / MYSQL数据库填充。在我的PHP中调用了10组数据(每组包含3个数据)。我的UITableView设置为发布3组。我的表中打印了3组数据,但它们都是相同的数据。如何打印最近的3套?

MainTableController.m

@implementation MainTableViewController
@synthesize textNeedTitle, textNeedPoster ,textNeedDescrip;

-(IBAction)unwindToRootVC:(UIStoryboardSegue *)segue
{
    // Nothing needed here.
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestTableViewCell *cell = (TestTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json2.php"];
    NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
    NSError *error;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];


    if(!error)
    {
        for (NSDictionary * needs in jsonArray)
        {
            cell.textNeedTitle.text = [needs objectForKey: @"needTitle"];
            cell.textNeedPoster.text = [needs objectForKey: @"needPoster"];
            cell.textNeedDescrip.text = [needs objectForKey: @"needDescrip"];
        }
    }

    else
    {
        textNeedTitle.text = [NSString stringWithFormat:@"Error--%@",[error description]];
    }
    return cell;
}

我的PHP设置为调用最近的10个帖子,但是我的Objective C中是否缺少必要的代码?

2 个答案:

答案 0 :(得分:2)

您正在cellForRow进行网络服务调用,每个单元格都会调用一次。

现在正在发生的是显示的单元格1 - >你下载数据 - >遍历它 - >并显示cell.textNeedTitle中的最后一个元素,即此代码

for (NSDictionary * needs in jsonArray)
        {
            cell.textNeedTitle.text = [needs objectForKey: @"needTitle"];
            cell.textNeedPoster.text = [needs objectForKey: @"needPoster"];
            cell.textNeedDescrip.text = [needs objectForKey: @"needDescrip"];
        }

您想要的是首先在viewDidLoad下载您的数据,这是您的视图完全初始化的时间。阅读更多文档。

所以改变你的代码就好了。

in .h file

interface MyClassVC:UIViewController {
   NSArray *myDataArray;
}

in .m file

-(void)viewDidLoad {
     [super viewDidLoad];   


    // get the data from the URL , you are only getting it synchronous currently. change to asynchronous . read the docs
    NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json2.php"];
    NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
    NSError *error;
    myDataArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];

    [tableView reloadData]; // if tableView is unidentified make the tableView IBOutlet
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return myDataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestTableViewCell *cell = (TestTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDictionary *needs = myDataArray[indexPath.row]; // get the data dict for the row
    cell.textNeedTitle.text = [needs objectForKey: @"needTitle"];
    cell.textNeedPoster.text = [needs objectForKey: @"needPoster"];
    cell.textNeedDescrip.text = [needs objectForKey: @"needDescrip"];

    return cell;
}

答案 1 :(得分:0)

除了Kabira的答案之外,它还可以使用异步加载来避免UI冻结。

添加了原始问题中建议的错误报告。

interface MyClassVC:UIViewController {
   NSArray *myDataArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json2.php"];
            NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
            dispatch_sync(dispatch_get_main_queue(), ^{
                NSError *error;
                jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
                if(error) {
                    textNeedTitle.text = [NSString stringWithFormat:@"Error--%@",[error description]];
                } else {
                    [tableView reloadData]; // if tableView is unidentified make the tableView IBOutlet
                }
            });
        });
    });

}