2011-08-23 135 views
-4

我無法找到解決方案如何在UITableViewCell中添加此「加載更多」按鈕。如何將一個按鈕添加到tableview的最後一行?

像 「更多結果...」 選項,在這個應用程序

http://itunes.apple.com/in/app/zdnet/id425580940?mt=8

希望有人能幫助我...

+0

你究竟想要做什麼。當你向下滾動表格時,你是否想要這樣做,然後加載更多出現,直到沒有新的結果。 – Gypsa

+0

我無法在Tableview中放置該按鈕。就像我在表格中有10行那樣,我怎樣才能在第11行放置「loadmore」選項。你可以給我一些鏈接或一些代碼。 – user755278

+0

可能重複的[在TableView中加載更多選項?](http://stackoverflow.com/questions/6936968/load-more-option-in-tableview) –

回答

0

而不是普通的內容,使得你的上單元格UIButton。當用戶單擊它時,用新信息更新數據源(不要忘記更新每個部分中的行數),並在tableview上調用reloadData。始終將按鈕保持在最底部的單元格中。

如何更新數據源完全取決於您的實施。

要將按鈕添加到一個UITableViewCell你可以的cellForRowAtIndexPath做到這一點:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (indexPath.row == dataForCells.count + 1){ // Assume dataForCells is an NSArray holding your data. Initially it should hold 10 objects, then after clicking this button it will be 20, 30, etc. as you load data in load10MorePressed. However, this depends on your implementation. You could be storing your data in some other way. 
     static NSString *CellIdentifier = @"LoadMoreCell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     } 

     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = CGRectInset(cell.frame, 5, 5); 
     [button setTitle:@"Load 10 More" forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(load10MorePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell addSubview:button]; 
    } else { 
     static NSString *CellIdentifier = @"NormalCell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     } 

     // Setup your normal data cells here 
    } 
} 

但是你要確保你有一個名爲load10MorePressed方法:

-(IBAction)load10MorePressed:(id)sender{ 
    NSMutableArray *temp = [NSMutableArray arrayWithArray:dataForCells]; 
    for (int x=0; x<10; x++){ 
     [temp addObject:[Create new objects here. Depends on your implementation, probably downloading from someplace...]]; 
    } 
    self.dataForCells = temp; 
    [self.tableView reloadData]; 
} 

最後,不要」 t忘記返回桌面視圖中的單元格數量:

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

    if (section == 0){ 
     return dataForCells.count + 1; 
    } else { 
     return 0; 
    } 
} 
+0

感謝您的回覆,但我是iPhone開發新手,所以我我無法在Tableview中放置該按鈕。就像我在表格中有10行那樣,那麼我如何在第11行放置「loadmore」選項。你可以給我一些鏈接或一些代碼。 – user755278

+0

我已經更新了我的回覆,以顯示如何創建單元格並加載更多數據。 – arsenius

+0

我以這種方式從XML獲取數據(我在每個頁面獲得10條新聞,有10個頁面,我通過在api中更改「%@」(佔位符)的值來獲取這些消息,所以我我再次無法做到這一點,我真的非常感謝你的回覆我工作的這個解決方案你給我,但我再次無法忍受這個問題(我有一個API像: - http://www.xyz.com /?page =%@我將這個佔位符「%@」的值改爲1到10,因爲每個頁面有10個新聞,每個頁面有10條新聞,所以我的問題是我能夠解析顯示在表格中,但是我如何設置10個新聞加載更多按鈕將顯示)... – user755278

0

As far a我的知識你可以這樣做。

把按鈕加載多了。當用戶點擊buttonAction中的按鈕時,取一個BOOL變量,然後使用[tableview reloadData];然後在numberofrowsintableview方法中檢查BOOL變量。如果是,則返回(Ex: )10 + previousrows.Pass數據也

0

我明白你的問題: - 你要做的是在表視圖下面添加一個活動指標。

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    cell.textLabel.text=[self.array objectAtIndex:indexPath.row]; 

    if (indexPath.row ==[array count] -1) { //this method will get called when you will scroll to the bottom of your table view 
     [self sendRequestMethod]; 

    } 
    else { 
     //NSLog(@"not nearest row"); 
     //[self remove]; 
    } 

    return cell; 
} 

-(void)sendRequestMethod 
{ 
    [self.newTableView setUserInteractionEnabled:NO]; 
    [indicator startAnimating];//make sure you start your indicator now perform what you want to do IN my case I am sending a request to server 

    NSString *str = [NSString stringWithFormat:@"http://www.google.com"]; 
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
    [request setRequestMethod:@"POST"]; 
    [request setDelegate:self]; 
    [request setDidFinishSelector:@selector(responseSuccedMethod:)]; 
    [request setDidFailSelector:@selector(responseFailedMethod:)]; 
    [networkQueue addOperation: request]; 
    [networkQueue go]; 


} 

-(void)responseSuccedMethod:(ASIHTTPRequest *)req 
{ 
    [self.newTableView setUserInteractionEnabled:YES]; 
    [indicator stopAnimating]; 
    [newTableView reloadData]; 

} 
+0

謝謝你的回覆,但我是新的iPhone開發,所以我無法將該按鈕放在Tableview中。就像如果我在表中有10行那麼我該如何放置該「loadmore」選項在第11排。你可以給我一些鏈接或一些代碼。 – user755278

2

也許做到這一點更簡單的方法是採用tableView:viewForFooterInSection:tableView:heightForFooterInSection方法,並與「加載更多結果」按鈕返回視圖。

-(id)init 
{ 
    self = [super init]; 
    if(self) { 
     // create a footer view so that we can add it later when we need to 
     loadMoreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; 
     UIButton* moreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [[moreButton titleLabel] setText:@"Load More Results"]; 
     [moreButton addTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside]; 
     [loadMoreView addSubview:moreButton];    

     // other init code here 
    } 
    return self 
} 

-(UIView*)tableView:(UITableView*)tv viewForFooterInSection:(NSInteger)s 
{ 
    return loadMoreView; 
} 
-(CGFloat)tableView:(UITableView*)tv heightForFooterInSection:(NSInteger)s 
{ 
    return [loadMoreView frame].size.height; // or 40 since that's what we set it to 
} 
+0

我認爲,這是一個更清潔的方法。但是你也需要爲UIButton指定框架; moreButton.frame = CGRectMake(0,0,160.0,40.0); – fatih

相關問題