Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What's the difference between directly setting the tableHeaderView/tableFooterView properties:

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

and implementing the viewForHeaderInSection/viewForFooterInSection methods?:

- (UIView *)tableView:(UITableView *)tableView 
    viewForHeaderInSection:(NSInteger)section
{   
    UIView *headerView = [[[HeaderView alloc] init...] autorelease];
    return headerView;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
521 views
Welcome To Ask or Share your Answers For Others

1 Answer

the first is the header of a table, the second will give you the oppertunity to add a header to every section in a table.

Green ist the tableViewHeader, while blue shows 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;
}

You will find the code for this App here: MyContacts


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...