可重用的TableView标题视图 [英] Reusable TableView header views

查看:81
本文介绍了可重用的TableView标题视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了性能的缘故,通常重用UITableView的单元格。
有没有办法做同样的事情与TableView头'视图?
我说的是与委托的方法返回的那些:

   - (UIView *)tableView: *)tableView viewForHeaderInSection:(NSInteger)section 

我试图做下面的正常工作:

   - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
static NSString * CellIdentifier = @Header;

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [self getHeaderContentView:CellIdentifier];
}
return cell;
}

有没有办法重复使用标题视图?

解决方案

苹果内置的重用tableview单元的能力的原因是,虽然tableview可能有很多行,但只有少数显示在屏幕上。



首先,头部视图只是UIViews,而UITableViewCell是一个子类,而不是为每个单元分配内存。的UIView,它们不打算放置为一个节头的视图。



此外,由于你通常会有比总行数少得多的节头,原因是构建一个可重用性机制,事实上Apple没有为通用的UIViews实现一个。



请注意,如果你只是设置标签到标题,你可以使用 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section



<自定义,例如带有红色文本(或按钮,图片等)的标签,您可以这样做:

  - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView * headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,44)] autorelease];
UILabel * label = [[[UILabel alloc] initWithFrame:headerView.frame] autorelease];
label.textColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@Section%i,section];

[headerView addSubview:label];
return headerView;
}


For performance sake it is usual to reuse UITableView' cells. Is there a way to do the same thing with TableView header' views? I am talking about the ones that are returned with delegate's method:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

I tried to do the following which doesn't seem to be working as expected:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *CellIdentifier = @"Header";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell == nil) {
        cell = [self getHeaderContentView: CellIdentifier];
    }
    return cell;
}

Is there a way to reuse header' views?

解决方案

The reason Apple built in the ability to reuse tableview cells is because while the tableview may have many rows, only a handful are displayed on screen. Instead of allocating memory for each cell, applications can reuse already existing cells and reconfigure them as necessary.

First off, header views are just UIViews, and while UITableViewCell is a subclass of UIView, they are not intended to be placed as the view of a section header.

Further, since you generally will have far fewer section headers than total rows, there's little reason to build a reusability mechanism and in fact Apple has not implemented one for generic UIViews.

Note that if you are just setting a label to the header, you can use -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section instead.

For something more custom, such as a label with red text (or a button, image, etc), you can do something like this:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease];
  UILabel *label = [[[UILabel alloc] initWithFrame:headerView.frame] autorelease];
  label.textColor = [UIColor redColor];
  label.text = [NSString stringWithFormat:@"Section %i", section];

  [headerView addSubview:label];
  return headerView;
}

这篇关于可重用的TableView标题视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆