【发布时间】:2012-01-31 21:21:16
【问题描述】:
我有一个 UITableView,其中数据源是使用 NSFetchRequest 的 Core Data。核心数据包含从我网站上的数据库下载的新闻项目。默认情况下,Core Data 包含 50 个最新项目,当到达 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 更改为如下图所示:
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;
}
【问题讨论】:
标签: objective-c ios cocoa-touch uitableview