2012-01-31 130 views
2

我有一個UITableView其中數據源是核心數據使用NSFetchRequest。核心數據包含從我的網站上的數據庫下載的新聞項目。核心數據在默認情況下包含50個最新的項目,並且在到達UITableView的底部時延遲下載較舊的項目。如何將自定義單元添加到UITableView的底部?

我目前正在使用tableView:numberOfRowsInSection方法處理這個問題,其中我爲最後一部分返回+1,這是我的「加載項目」單元格。在tableView:cellForRowAtIndexPath方法中,當IndexPath是最後一節中的最後一行時,我創建了「加載項目」單元格。

問題是,當新數據下載之前是「加載項目」單元格的單元格現在是常規新聞項目單元格。但是當細胞離開細胞並回來時,細胞首先被重新加載。

當延遲加載完成時,我可以存儲「加載項目」單元格的indexPath並重新加載該單元格。但我想知道,如果有任何更好的解決方案存在這個問題?

EDIT1:

這裏是我創造了 「加載項」 的UITableViewCell程序。

cell.textLabel.text = [NSString stringWithFormat:@"%@\u2026", NSLocalizedString(@"LOADING_MORE", nil)]; 

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

UIImage *spacer = [UIImage imageNamed:@"Spacer"]; 

UIGraphicsBeginImageContext(spinner.frame.size); 
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)]; 
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

cell.imageView.image = resizedSpacer; 
[cell.imageView addSubview:spinner]; 
[spinner startAnimating]; 

EDIT2:

我試圖 「設計」 的footerView使用下面的代碼。目前活動指標位於左上角位置,標籤不可見。

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { 
    NSInteger sectionsAmount = [tableView numberOfSections]; 
    if (section == sectionsAmount - 1) { 
     return 20; 
    } 
    return 0; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
    NSInteger sectionsAmount = [tableView numberOfSections]; 
    if (section == sectionsAmount - 1) { 
     if (_tableFooterView==nil) { 
      UIView *view = [[UIView alloc] init]; 

      UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

      UIImage *spacer = [UIImage imageNamed:@"Spacer"]; 

      UIGraphicsBeginImageContext(spinner.frame.size); 
      [spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)]; 
      UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsEndImageContext(); 

      UIImageView *imageView = [[UIImageView alloc] init]; 

      imageView.image = resizedSpacer; 
      [imageView addSubview:spinner]; 
      [spinner startAnimating]; 

      [view addSubview:imageView]; 

      UILabel *label = [[UILabel alloc] init]; 
      label.text = [NSString stringWithFormat:@"%@\u2026", NSLocalizedString(@"LOADING_MORE", nil)]; 
      [view addSubview:label]; 

      _tableFooterView = view; 
     } 
     return self.tableFooterView; 
    } 
    return nil; 
} 

我如何可以改變的UIView看起來像下面的圖片: Loading more..

EDIT3:

這是我setupTableFooterView這裏我指定一個視圖的tableFooterView。該方法從viewDidLoad調用。

- (void) setupTableFooterView { 
    UIView *view = [[UIView alloc] init]; 

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

    UIImage *spacer = [UIImage imageNamed:@"Spacer"]; 

    UIGraphicsBeginImageContext(spinner.frame.size); 
    [spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)]; 
    UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageView *imageView = [[UIImageView alloc] init]; 

    imageView.image = resizedSpacer; 
    [imageView addSubview:spinner]; 
    [spinner startAnimating]; 

    imageView.frame = CGRectMake(10, 11, 20, 20); 
    [view addSubview:imageView]; 

    UILabel *label = [[UILabel alloc] init]; 
    label.text = [NSString stringWithFormat:@"%@\u2026", NSLocalizedString(@"LOADING_MORE", nil)]; 
    [label setFont:[UIFont italicSystemFontOfSize:13]]; 
    [label setFrame:CGRectMake(40, 0, 270, 43)]; 
    [view addSubview:label]; 

    self.tableView.tableFooterView = view; 
} 

回答

5

使用UITableViewtableFooterView代替該消息。這樣你就不必擺弄節/行結構。

UITableView reference

tableFooterView 

返回所顯示的表下方的附件圖。

@property(nonatomic, retain) UIView *tableFooterView 

討論

默認值是零。表腳註視圖與 小節頁腳不同。

+0

感謝您的評論,我認爲這可能是我的解決方案。我嘗試設置tableFooterView失敗。你能看看我的EDIT2嗎? – dhrm 2012-01-31 22:28:10

+0

您正在植入一款Footer,我當時建議使用tableFooter。當然,一個sectionFooter是一個選項,但我會建議使用tableFooter來代替。請檢查鏈接的參考。 – Till 2012-01-31 22:38:47

+0

對不起,你在哪裏沒錯。我已將視圖初始化添加到* viewDidLoad *。你能看到我的'EDIT3'嗎? – dhrm 2012-01-31 22:47:10

0

你在懶惰的下載回調中沒有使用[tableView reloadData]嗎?因此,當服務器發回更多結果並將它們添加到數據源時,表格繪製邏輯會被重新調用,並且numberOfRowsInSection的添加會得到恰當的解釋嗎?

此外,我認爲你只是將表格的最後一個單元格指定爲「加載單元格」的建議可能會正常工作。合成一個保留的屬性或成員變量並在其中存儲您的LoadCell引用,並在調用所有單元創建邏輯之前檢查indexPath。如果它是表格中的最後一個單元格,則返回self.loadingCell。

0

我假設你目前在你的表格視圖中只有一個部分。改爲製作兩個部分。第一部分只包含正常的數據行。第二部分僅包含「加載項目」單元格。

當新數據可用時,使用insertRowsAtIndexPaths:withRowAnimation:通知表格視圖新行。

+0

好建議,upvoted – Justin 2012-01-31 21:29:04

+0

對不起,但我有多個部分。 – dhrm 2012-01-31 21:35:00

相關問題