滚动时更新表值

时间:2015-12-04 07:55:58

标签: ios objective-c uitableview

当表格被加载以及滚动时,我需要在单元格中显示数据。我在第一次运行应用程序时将数据输入到表行中,但是当我滚动表时,表不会更新所有值。它仅显示服务的最后一次获取值。

我的服务请求格式为:

http://vapp.sites.net/......userId=967730&rowsPerPage=10&nextCursorMark=1

注意 - 当我呼叫服务时,NextCursorMark会在响应时增加,我会在滚动时传递给下一个服务调用。当nextCursorMark为nil时,我必须停止滚动表,因为没有更多新的响应数据。

回应:

{
 MESSAGE: "Success",
 STATUS_CODE: 200,
 REQUEST: [ ],
 RESPONSE: {
 OrderList: [..<items>..],
 nextCursorMark: "2",
 totalOrderCount: 45
 }
 }

当第一次加载表时nextCursorMark值为1,并且在每次滚动时,值将根据totalOrderCount增加。如何在每个滚动滚动并调用服务时管理表视图数据源?

我的代码如下:

1 - 加载表格时

#pragma mark - UITableViewDataSource Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
return self.arrOrderList.count;
}

 - (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
 {
OrderList * objOrderList = (OrderList *)[self.arrOrderList objectAtIndex:section];
return objOrderList.orderDetailsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"OrderDetailsListCell";
OrderDetailsListCell * cell = (OrderDetailsListCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell = [[OrderDetailsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
OrderList * objOrderList = (OrderList *)[self.arrOrderList objectAtIndex:indexPath.section];
OrderDetailsList * objOrderDetailsList = (OrderDetailsList *)[objOrderList.orderDetailsList objectAtIndex:indexPath.row];
[cell loadDataWithOrderDetailList:objOrderDetailsList];
return cell;
 }



- (void) ServiceCallForOrderHistoryWithCurrentPageMark:(NSString *)page
{
if(![[VCSpinnerView sharedInstance] isSpinnerAddedToTheView:self.view])
    [[VCSpinnerView sharedInstance] showInView:self.view];

__block __weak VCMyOrderViewController* blockMyOrderListView = self;

[[VCModelManager sharedInstance] getMyOrderDetail:[[VCConfiguration sharedConfig] getLoggedInUserId] andPageNumber:page completionBlock:^(id result, NSError *error)
 {
        if (!error)
            {
                blockMyOrderListView.orderHistoryDataObject = (OrderHistoryBaseClass *)result;
                dispatch_async(dispatch_get_main_queue(), ^{
                [blockMyOrderListView setDataSourceMyOrders:blockMyOrderListView.orderHistoryDataObject.rESPONSE];});
            }
        else
            {
                NSLog(@"\nError->%@",[error debugDescription]);
            }
        dispatch_async(dispatch_get_main_queue(), ^{
        [[VCSpinnerView sharedInstance] removeSpinnerView];
     });
  }];
}

- (void)setDataSourceMyOrders:(OrderRESPONSE *)response
{
self.orderHistoryDataObject.rESPONSE = response;

[self setPageMark:self.orderHistoryDataObject.rESPONSE.nextCursorMark];
[self setPageCount:[self.orderHistoryDataObject.rESPONSE.totalOrderCount integerValue]];
self.arrOrderList = [NSMutableArray arrayWithArray:self.orderHistoryDataObject.rESPONSE.orderList];

TrackOrderView *trackOrderView = [[TrackOrderView alloc] init];
[trackOrderView setDelegate:self];
[self.tblMyOrder setTableHeaderView:trackOrderView];
[self.tblMyOrder reloadData];
}

2 - 滚动表格时

- (void) scrollViewDidScroll:(UIScrollView *)scrollView
 {
   if (self.arrOrderList.count < self.pageCount)
     {
      [self ServiceCallForOrderHistoryWithCurrentPageMark:self.pageMark];
     }
  }

1 个答案:

答案 0 :(得分:0)

你需要:

  1. 拥有一个可变数组来存储完整列表
  2. 有变量来存储下一个URL(或下一个光标标记)
  3. 在viewDidLoad中,将mutable数组设置为空数组,并将URL设置为初始URL
  4. 仍然在viewDidLoad中,调用您的方法来加载更多数据
  5. 此方法应将收到的数据附加到您的可变数组,使用下一个要更新的URL更新URL(如果没有更多数据,则更新为nil)
  6. 该方法应调用beginUpdates,然后调用insertRowsAtIndexPaths:withRowAnimation:,然后调用endUpdates
  7. 在scrollViewDidScroll中,根据表格大小和可见大小检查滚动偏移量,如果您接近结尾,请调用方法以加载更多数据。
  8. 当然,当要加载的URL为nil
  9. 时,加载其他数据的方法应立即退出