【问题标题】:Display records of next page in json in uitableview on scrolling the tableView when I reach the last record当我到达最后一条记录时,在 uitableview 中滚动 tableView 时在 json 中显示下一页的记录
【发布时间】:2015-05-05 06:15:58
【问题描述】:

我正在尝试制作一个使用 SBJSON 和 ASIHTTPRequest 的应用程序,该应用程序在 json 中包含 273 页(每页 50 条记录)。当我滚动 tableView 时,我想在到达最后一条记录时获取下 50 条记录。目前我只得到前 50 条记录。我将如何获得所有记录?我正在使用 Xcode 6 目标 c。

//Here I am taking the json data on view load

- (void)viewDidLoad
{
    [super viewDidLoad];
    main_array = [[NSMutableArray alloc] init];
    NSURL *countryURL = [NSURL URLWithString:@"http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=json"];

    ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:countryURL];

    [request setDelegate:self];

    [request startSynchronous];

    [self setTitle:@"COUNTRIES"];
}


-(void) requestFinished: (ASIHTTPRequest *) request
{
    NSString *theJSON = [request responseString];

    SBJsonParser *parser = [[SBJsonParser alloc] init];

    NSMutableArray *jsonDictionary = [parser objectWithString:theJSON error:nil];

    //Here I am separating the different arrays and storing it in model class

    NSDictionary *pagesDict = [jsonDictionary objectAtIndex:0];
    NSMutableArray *dataArray = [jsonDictionary objectAtIndex:1];

    for (NSDictionary *dictionary in dataArray)
    {
        Model *model = [[Model alloc]init];

        NSDictionary *tmpdict =[dictionary objectForKey:@"indicator"];
        NSDictionary *tmpcountdict =[dictionary objectForKey:@"country"];

        model.indicator = [tmpdict objectForKey:@"value"];
        model.country = [tmpcountdict objectForKey:@"value"];
        model.date = [dictionary objectForKey:@"date"];
        model.value = [dictionary objectForKey:@"value"];
        model.decimal = [dictionary objectForKey:@"decimal"];
        //main_array is the main mutable array to store values of model class
        [main_array addObject:model];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [main_array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *CellIdentifier = @"SimpleTableViewCell";
    SimpleTableViewCell *cell = (SimpleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    Model *model = [[Model alloc]init];
    model = [self.main_array objectAtIndex:indexPath.row];

    //Display value in cells

    cell.indicatorLabel.text = [NSString stringWithFormat:@"%@",model.indicator];
    cell.countryLabel.text = [NSString stringWithFormat:@"%@",model.country];
    cell.yearLabel.text = [NSString stringWithFormat:@"%@",model.date];
    cell.valueLabel.text = [NSString stringWithFormat:@"%@",model.value];
    cell.decimalLabel.text = [NSString stringWithFormat:@"%@",model.decimal];

    return cell;
}

【问题讨论】:

    标签: objective-c uitableview xcode6 asihttprequest sbjson


    【解决方案1】:

    您是否尝试过在查询中使用“页面”参数? http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=json&page=2

    如果要列出所有记录,可以使用“per_page”参数,我不推荐 http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=json&per_page=13640

    【讨论】:

    • 'page' 参数工作正常,但是当我尝试使用 'per_page' 参数时我得到空白表格视图。
    • 您确定要一次下载所有这些记录吗?您可以一次加载 150 条记录,并在滚动 tableview 时加载其他记录。我也猜想数据可以在服务器上为你排序。
    • 我想在滚动表格视图时加载数据。我刚开始使用 iOS,对如何操作并没有太多想法。任何帮助将不胜感激。
    【解决方案2】:

    您可以在最后一个单元格上设置一些标签,然后在到达该单元格后向服务器发送呼叫。从服务器接收到数据后,将其附加到表视图数据并重新加载数据。

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
        if(indexPath.row == [data count])
        {
    
            cell.tag = SOME_TAG_OF_LAST_CELL;
        }
    
        return cell;
    }
    
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(cell.tag == SOME_TAG_OF_LAST_CELL)
        {
            // send call to server to fetch next page from server.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2013-09-10
      相关资源
      最近更新 更多