2014-03-02 15 views
1

目前嘗試使用這個插件來實現無限滾動到我的應用程序:https://github.com/pronebird/UIScrollView-InfiniteScroll我試圖實現無限滾動到我的UITableViewController和不確定如何使用數據庫結果返回

到目前爲止,我已經添加了這個代碼我的tableview控制器viewDidAppear和viewDidDisappear方法:

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    // setup infinite scroll 
    // keep a weak reference to table view 
    __weak UITableView *weakTableView = self.tableView; 

    [self.tableView addInfiniteScrollWithHandler:^{ 
     // keep a strong reference to table view 
     __strong UITableView *strongTableView = weakTableView; 

     // seems like our table view didn't make it 
     if(strongTableView == nil) return; 


     // 
     // fetch your data here, can be async operation, 
     // just make sure to call finishInfiniteScroll in the end 


     // finish infinite scroll animation 
     [strongTableView finishInfiniteScroll]; 
    }]; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 

    // remove infinite scroll 
    [self.tableView removeInfiniteScroll]; 
    [[self tableView] reloadData]; 
} 

我拖動表和微調顯示了最後一排的下方和後一兩秒鐘消失。現在我需要做的就是從我的數組中獲取數據,並將其添加到viewDidAppear代碼中。

這是我目前得到我parse.com數據到一個名爲NSMuteableArray實例「人」:

- (void)populatePeopleArrayWithCloudData { 
    // Grab data for datasource and store in people array 
    NSLog(@"view did load"); 
    people = [[NSMutableArray alloc] init]; 


    PFQuery *query = [PFQuery queryWithClassName:@"People"]; 
    [query whereKey:@"active" equalTo:@1]; 
    [query orderByDescending:@"createdAt"]; 
    [query setLimit:10]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 

     if (!error) { 
      for (PFObject *object in objects) { 

       Person *person = [[Person alloc] init]; 
       [person setName:[object objectForKey:@"name"]]; 
       [person setNotes:[object objectForKey:@"notes"]]; 
       [person setAge:[[object objectForKey:@"age"] intValue]]; 
       [person setSince:[object objectForKey:@"since"]]; 
       [person setFrom:[object objectForKey:@"from"]]; 
       [person setReferenceNumber:[object objectForKey:@"referenceNumber"]]; 

       PFFile *userImageFile = object[@"image"]; 
       [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) { 
        if (!error) { 
         UIImage *image = [UIImage imageWithData:imageData]; 
         [person setImage:image]; 
        } 
       }]; 

       [person setActive:[[object objectForKey:@"active"] intValue]]; 
       [person setObjectId:[object objectId]]; 
       [people addObject:person]; 
      } 

     } else { 
      // Log details of the failure 
      NSLog(@"Error: %@ %@", error, [error userInfo]); 
     } 

     NSLog(@"Calling reloadData on %@ in viewDidLoad", self.tableView); 
     [self.tableView reloadData]; 

    }]; 

} 

我將結果限制爲10。現在我希望要做的就是繼續抓住下一個每次滾動到表格底部時,還沒有抓取10個結果。這個代碼可以幫助我做到這一點,需要在上面提到的塊中進行。

「人」的實例使用我的tableviewdatasource方法:

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

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [[self tableView] dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

     // Configure the cell... 

     Person *current; 
     if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
      current = [searchResults objectAtIndex:indexPath.row]; 
     } else { 
      current = [people objectAtIndex:[indexPath row]]; 
     } 

     [[cell textLabel] setText: [current name]]; 
     [[cell imageView] setImage: [current image]]; 
     [[cell detailTextLabel] setText: [current notes]]; 

    return cell; 
} 

我如何使用我的數據庫的結果與這個插件?正如你所看到的,我將結果限制爲10,當我滾動到表格底部並將它們添加到表格最後一行之後時,我需要抓住下10個結果。

親切的問候

更新 - 我的切法行數,因爲它代表:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 


    if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
     return [searchResults count]; 

    } else { 
     return [people count]; 
    } 

} 

回答

0

快樂幫,但你應該給它一個鏡頭首先爲我們提供反饋。有幾個想法,讓你去...

的總體思路是使用「跳過」屬性上PFQuery獲得下一個10每次調用它的時候,你加10

所以創建查詢,將它放在屬性中,但將findObjectsInBackgroundWithBlock調用移動到您的infiniteScrollHandler,在您調用它之後每次添加10跳過。然後在處理結束時(現在稱爲重新加載表格),請致電[strongTableView finishInfiniteScroll]

在您的numberOfRows中,您必須提供您的源中可用的最大人數。

+0

我試着想如何把它放在一起,它的做法稍微有點:)。首先,我創建了一個PFQuery類型的capturedQuery實例變量。我將在塊運行之前使用它來捕獲查詢。 – LondonGuy

+0

我注意到你會有兩個級別的「塊時間」(塊內的塊)。可能最好先使用findObjects:然後擔心在後臺執行搜索。 – mackworth

+0

你得到它的工作? – mackworth