【问题标题】:UItableView load data on scrollUItableView 在滚动时加载数据
【发布时间】:2012-02-01 07:01:37
【问题描述】:

在我的应用程序中,我从网络服务获取数据,我必须在 UITableView 中显示它。 但这里的条件是我最初只能显示 10 条记录,然后一旦用户向下滚动,我就必须加载更多记录。我尝试搜索它但没有得到任何有用的答案。 我同意我将使用 -

(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

显示值,但我将如何从服务中仅获取 10 条记录,然后基于滚动获取其他记录。 请提供一些指针或示例代码。

谢谢

【问题讨论】:

  • 试试lazyLoading概念怎么样?
  • 不,我不想实现延迟加载,我希望一次加载 10 条记录。

标签: iphone uitableview


【解决方案1】:

如果有人需要它,我可以通过这种方式解决我的问题。 首先,您需要以这样的方式配置服务器,以便它应该根据 TableView 中可见的行一次返回 10 个数据。 这是 tableView 委托,它被调用并返回 tableView 中的可见单元格

 -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        int lastRow=[nameArray count]-1;
        if(([indexPath row] == lastRow)&&(lastRow<[categoryArray count]))  
        {

            if(tableView==m_pDetailsTableView)    {
                savedScrollPosition=lastRow;
                startCellValue=[NSString stringWithFormat:@"%d",0];
                endCellValue=[NSString stringWithFormat:@"%d",[nameArray count]+10];
                [self connectToServer]; //Method to request to server to get more data
            }
        }
    }

savedscrollPosition 变量将变量存储为您希望在加载数据后滚动表格视图的点。

【讨论】:

    【解决方案2】:

    您应该阅读有关延迟加载的内容。该代码可在 Apple 的网站上找到。在这里下载

    http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html

    检查代码

    - (void)loadImagesForOnscreenRows 
    

    方法。

    它使用您需要的相同方法。它获取 table view 的当前滚动位置,并在此基础上获取屏幕上显示的单元格及其 indexPath。在此基础上,您将能够显示屏幕中显示的那些单元格。

    为了显示 10 行,需要进行简单的计算。

    【讨论】:

    • 是的,我同意,我可以应用逻辑来显示它,但为此我还必须立即从 Web 服务加载整个数据。?我想从服务中获取 10 条记录并显示。
    • NO ,不是整个数据,只是属于一次显示在屏幕上的单元格的那些数据。所以首先在查看表时加载您被要求的 10 条记录。之后只获取那些单元格显示在屏幕上的记录。延迟加载意味着只加载需要的信息,而不是整个数据。
    • 如果您可以在“iPhone”聊天室加入聊天,我们可以详细讨论。很高兴为您提供帮助
    【解决方案3】:

    只需将新数据插入您的数据源 见下文

    如果您使用的是 xml - 请查看 XMLReader - turn XML into an NSDictionary 下面的示例代码使用 AFNetworking(非阻塞) https://github.com/AFNetworking/AFNetworking/

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
        if (!decelerate)
        {
            [self fetchMoreData];
        }
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        [self fetchMoreData];
    }
    
    
    - (void)fetchMoreData
    {
    
        if ([resultArray count] > 0)
        {
           NSArray *visiblePaths = [myTableView indexPathsForVisibleRows];
           NSIndexPath *lastRow = [visiblePaths lastObject];
    
            // Check whether or not the very last row is visible.
            NSInteger numberOfSections = [myTableView numberOfSections];
            NSInteger lastRowSection = [lastRow section];
            NSInteger lastRowRow = [lastRow row];
            NSInteger numberOfRowsInSection = [myTableView numberOfRowsInSection:lastRowSection];
    
            if (lastRowSection == numberOfSections - 1 &&  
                lastRowRow== numberOfRowsInSection - 1) {
    
                DLog(@"it's the last row");
                if ([resultArray count]%10 == 0) { // use a divider based on your pagination
                   [self fetchNextPage];
                }
    
            }
        }
    }
    
    
    -(void)getFeeds{
        ENTER_METHOD;
    
        [resultArray removeAllObjects];
        //reset this
       NSString *url = [NSString stringWithFormat:@"/webserviceurl.xml?offset=0"];
        [httpClient getPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
            [self parseFeedsXMLString:operation.responseString];
            //  offset = offset + 10; // ONLY if there's results increment
    
        } failure:^(AFHTTPRequestOperation *operation, id responseObject){
            NSString *detailError=nil;
         }];
    
    }
    
    -(void)fetchNextPage{
    
        NSString *url = [NSString stringWithFormat:@"/webserviceurl.xml?offset=%d",offset];
        [httpClient getPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
    
            DLog(@"operation.responseString:%@",operation.responseString);
            [self parseNextFeedsXMLString:operation.responseString];
           // offset = offset + 10; // ONLY increment if there's results 
    
    
        } failure:^(AFHTTPRequestOperation *operation, id responseObject){
    
        }];
    
    }
    
    
    
    - (void)parseFeedsXMLString:(NSString *)xmlString
    {
    
        NSError *parseError = nil;
        NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:xmlString error:&parseError];
        DLog(@"xmlDictionary:%@",xmlDictionary);
        resultArray = [[NSMutableArray arrayWithArray:[[xmlDictionary objectForKey:@"feed"] objectForKey:@"entry"]]retain];
    
        [myTableView reloadData];
    }
    
    -(void)parseNextFeedsXMLString:(NSString *)xmlString
    {
    
        NSError *parseError = nil;
        NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:xmlString error:&parseError];
        DLog(@"xmlDictionary:%@",xmlDictionary);
        //[resultArray insertObject:e atIndex:[resultArray count]];
    
        NSMutableArray *results  = [NSMutableArray arrayWithArray:[[xmlDictionary objectForKey:@"feed"] objectForKey:@"entry"]];
    
        if ([results count]) {
            page++;
            for (NSDictionary  *dict in results) {
                [resultArray insertObject:dict atIndex:[results count]];
            }
    
        }
        [myTableView reloadData];
    }
    

    【讨论】:

      【解决方案4】:

      如果我正确理解您的问题,您可以执行以下操作。

      1) 实现scrollViewDidScroll

      2 ) 检查其中的可见行

      3 ) 如果您找到最后一行,只需调用 Web 服务进行加载 更多数据

      4 ) 获取数据时只需重新加载表格

      试试看。

      【讨论】:

        【解决方案5】:

        可以调整tableView:numberOfRowsInSection:方法的返回值,每次插入十行,返回值加10。

        对不起我的英语不好。

        【讨论】:

        • 但是我仍然必须一次加载数组中的整个数据。对吧..?但是我想获取 10 条记录并显示。
        • 我认为问题很普遍;每次可以请求很多条记录(10条以上);但是您可以调整 numberOf*** 来控制 tableView 中的单元格编号,当您滚动到底部时,您可以再插入另外 10 个单元格,如果记录不在本地,则向 web-server 再次请求新记录需要。
        • 没错 Yebw。我想向网络服务器发出另一个请求以获取另外 10 条记录。任何想法如何做到这一点只是为了获取 10 条记录。
        • 我认为是你与服务器必须支持的通信协议
        【解决方案6】:
         - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
        NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
        if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
            i = i +10;
            NSString *unescaped = mySearchBar.text;
            NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
            [PServiceAPI searchKeyWord:escapedString withOffSet:i Handler:^(NSArray *results, NSError *error) {
                if (error == nil) {
                    [arBase addObjectsFromArray:results];
                    [myTableView2 reloadData];
        
                }
            }];
        
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-22
          相关资源
          最近更新 更多