簡體   English   中英

如何將自定義單元格添加到UITableView的底部?

[英]How to add a custom cell to the bottom of a UITableView?

我有一個UITableView ,其中數據源是使用NSFetchRequest的核心數據。 核心數據包含從我網站上的數據庫下載的新聞項目。 默認情況下,當到達UITableView的底部時,核心數據默認包含50個最新項,而較舊的項則被延遲下載。

我目前正在使用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更改為下圖所示: 正在加載更多..

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;
}

對該消息使用UITableView tableFooterView代替。 這樣一來,您就不必擺弄橫斷面/行結構。

UITableView參考

tableFooterView

返回顯示在表格下方的附件視圖。

@property(nonatomic, retain) UIView *tableFooterView

討論

缺省值為nil。 表格頁腳視圖與部分頁腳不同。

您是否在懶惰的下載回調中使用[tableView reloadData]? 因此,當服務器發回更多結果並將它們添加到您的數據源中時,將重新調用表繪制邏輯,並正確考慮對numberOfRowsInSection的添加?

另外,我認為您的建議是將表的最后一個單元格分配為“加載單元格”可能會很好。 合成保留的屬性或成員變量,並將其中的LoadingCell引用存儲在其中,只需在調用所有單元格創建邏輯之前檢查indexPath。 如果它是表中的最后一個單元格,則只需返回self.loadingCell。

我假設您當前在表格視圖中只有一個部分。 改為制作兩個部分。 第一部分僅包含普通數據行。 第二部分僅包含“正在加載項目”單元格。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM