objective-c - UITableView - 直接设置 tableHeaderView 属性与实现 -viewForHeaderInSection 方法

标签 objective-c ios cocoa-touch uitableview

直接设置tableHeaderView/tableFooterView属性有什么区别:

UIView *headerView =  [[UIView alloc] init...];
tableView.tableHeaderView = headerView;
[headerView release];

并实现 viewForHeaderInSection/viewForFooterInSection 方法?:

- (UIView *)tableView:(UITableView *)tableView 
    viewForHeaderInSection:(NSInteger)section
{   
    UIView *headerView = [[[HeaderView alloc] init...] autorelease];
    return headerView;
}

最佳答案

第一个是表格的标题,第二个会让您有机会为表格中的每个部分添加一个标题。

绿色是 tableViewHeader,蓝色是 sectionHeaders。

enter image description here

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (headerView == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"DetailContactHeader" owner:self options:nil];
        headerView.nameLabel.text = [NSString stringWithFormat:@"%@ %@", 
                                                   [contact objectForKey:@"name"],
                                                   [contact objectForKey:@"familyname"]];
        if ([[contact allKeys] containsObject:@"pictureurl"]) {
            headerView.avatarView.image = [UIImage imageNamed:[contact objectForKey:@"pictureurl"]];
        }
    }
    [self.tableView setTableHeaderView: headerView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [[contact allKeys] count]-3;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView       
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
                                       reuseIdentifier:CellIdentifier] autorelease];
    }

    id key = [self.possibleFields objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", key];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [contact objectForKey:key]];
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView 
  heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}

-(UIView *) tableView:(UITableView *)tableView 
viewForHeaderInSection:(NSInteger)section
{
    UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    l.backgroundColor = [UIColor clearColor];
    l.text= @"I am a Section Header";
    return l;
}

您可以在此处找到此应用程序的代码:MyContacts

关于objective-c - UITableView - 直接设置 tableHeaderView 属性与实现 -viewForHeaderInSection 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6446979/

相关文章:

ios - UITableView 的委托(delegate)和数据源方法

objective-c - 如何处理仅从键盘在 ObjC 源代码中键入嵌套消息调用?

iOS,iOS9+ 提供原生模糊 View 效果?

objective-c - UIManagedDocuments 获取新数据后 NSFetchedResultsController 不更新 TableView

objective-c - UIPopover已重新定位,但位置错误

objective-c - 如何保持调整大小的窗口的纵横比?

ios - 什么会触发 viewDidLayoutSubviews 并寻找一个好的模式来启动 subview 框架

ios - 使用 xcode 将 IOS 静态库架构设置为 "arm64"

objective-c - UISegmentedControl alpha 属性无法正常工作

ios - swift 改变颜色的属性观察器