2012-01-31 123 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

相关问题