2012-12-28 17 views
8

下面是我用來獲取和解析Feed的不同頁面的函數。UITableView插入更多的行,同時保持已獲取的人

- (void)getFeed:(NSInteger) pageNumber 
{ 
    collectedData = [[NSMutableData alloc] init]; 
    NSString *urlStr =[NSString stringWithFormat:@"http://sitename.com/feed/?paged=%d", pageNumber]; 
    NSURL *url = [NSURL URLWithString:urlStr]; 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 
    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; 
} 

當用戶向下滾動到的tableview的底部,我用下面的函數通過增加計數器來訪問飼料的第二頁等等:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height; 
    if (endScrolling >= scrollView.contentSize.height) 
    { 
     counter ++; 
     [self getFeed:counter]; 
    } 
} 

的問題是,當代替將它加載到已獲取的項目下面,它重新加載tableview以顯示新的頁面項目,並且舊的項目消失。我想在現有的下面加載新的頁面項目以進行無限滾動。我怎樣才能做到這一點?

回答

2

回答你自己的問題感覺不好,但從另一個論壇的幫助,我能夠找出答案。

tableview控制器沒有與可變數組同步,所以我創建了另一個並追加了這兩個數組。

第一步:我創建了一個屬性

@property (nonatomic, readonly) NSMutableArray *moreItems; 

第二步:初始化在viewDidLoad中陣列主叫fetchEntries

moreItems = [[NSMutableArray alloc] init]; 

之前步驟3:在connectionDidFinishLoading,我所附新數組 'moreItems' 到前一陣列'items'

[moreItems addObjectsFromArray:[channel items]]; 

第4步:更改了n中的數據源umberOfRowsInSection從

return [[channel items] count]; 

到新陣列

return [moreItems count]; 

,然後在的cellForRowAtIndexPath,使用了新的陣列

RSSItem *item = [moreItems objectAtIndex:[indexPath row]]; 
1

每當你的陣列得到一個新的對象添加你調用這個

-(void)insertRowInTableView 
{ 
    int row = [YourDataSourceArray count]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0]; 
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; 
    [YourTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop]; 
} 

這裏YourDataSourceArray是數組要從中設置你的tableview行計數/數據。 YourTableView是您的tableView對象名稱。

+0

我知道,但我不知道如何使用此功能 – Jessica

+0

剛剛編輯我的回答 – spider1983

+0

lemme試試這個 – Jessica

0

這可能是因爲,當你從web服務獲取數據時,你將它添加到你用來填充表的數組中,這樣你將只有新數據,你必須做的是你有追加新的數據到陣列(使它可變數組並追加新的數據),只需使用以下行

[your_Table reloadData]; 

希望這會幫助你。

+0

我已經這樣做了,但那不是解決我的問題。我不得不使用insertRowsAtIndexPaths,但我不知道如何 – Jessica

+0

你想你的新數據在特定的行。 – superGokuN

+0

在已獲取行的末尾 – Jessica

1

您應該使用insertRowsAtIndexPaths: withRowAnimation:方法UITableView。你必須照顧

的一件事是,當你使用此方法,通過tableView:numberOfRowsInSection重新調整行數應該等於number_of_existing_rows + number_of_newly_inserted_rows

0

而不是使用向下滾動加載的,最好使用tableview中的最後一行是「加載更多項目「,當用戶選擇該行時,獲取新的提要並重新加載桌面視圖。

而且我在你的代碼中有一個疑問,每次在getfeed方法中,你正在創建數組
collectedData。我懷疑你錯過了將這些數據附加到表數據源數據,這可能會導致只顯示tableview中最近的feed數據。請檢查

+0

使用tableviewfooter而不是滾動到底部也是一種可能性。我認爲現在我必須關注主要問題 – Jessica

+0

不,這是masterdetail模板的默認代碼,它只是在頂部添加行 – Jessica

+0

嗨,請檢查下面的代碼。首先,您需要在數據源中插入數據並創建調用insertRowsAtIndexPaths:withRowAnimation。 –

1

完成檢索下一頁的新提要後,獲取數組中的對象。 將這個對象從這個數組添加到你的數據源數組中。 現在插入行表如下:

[yourTable beginUpdates]; 
[yourTable insertRowsAtIndexPaths:indexPathsToInsert 
       withRowAnimation:UITableViewRowAnimationBottom]; 
[yourTable endUpdates]; 

其中「indexPathsToInsert」是從最後一排你的表開始,方含indexPaths爲您的新對象NSIndexPath對象的數組。

希望這會有所幫助。

1

嗨,

創建for循環,與起始索引是(在數據源對象的號碼),您要添加(計數)對象的數量,創造indexpaths的陣列,並呼籲insertRowsAtIndexpaths:withRowAnimation。我希望代碼可以幫助你。

NSInteger statingIndex = [self.tableViewDatasource count]; 
    NSInteger noOfObjects = //Get the no.of objects count from getFeed method. 
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init]; 

    for (NSInteger index = statingIndex; index < statingIndex+noOfObjects; index++) { 

     [_objects addObject:]; // add the object from getFeed method. 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0]; 
     [indexPaths addObject:indexPath]; 
     [indexPath release]; 

    } 

    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; 
+0

也請查看這一個 – Jessica

相關問題