繁体   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