我有一个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看起来像下面的图片:
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;
}
感谢您的评论,我认为这可能是我的解决方案。我尝试设置tableFooterView失败。你能看看我的EDIT2吗? – dhrm 2012-01-31 22:28:10
您正在植入一款Footer,我当时建议使用tableFooter。当然,一个sectionFooter是一个选项,但我会建议使用tableFooter来代替。请检查链接的参考。 – Till 2012-01-31 22:38:47
对不起,你在哪里没错。我已将视图初始化添加到* viewDidLoad *。你能看到我的'EDIT3'吗? – dhrm 2012-01-31 22:47:10