2011-06-22 8 views
7

क्या सीधे tableHeaderView/tableFooterView गुण स्थापित करने के बीच का अंतर है:UITableView - सीधे बनाम कार्यान्वयन -viewForHeaderInSection विधि tableHeaderView संपत्ति की स्थापना

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; 
} 

उत्तर

37

पहला टेबल का शीर्षलेख है, दूसरा आपको तालिका में प्रत्येक अनुभाग में शीर्षलेख जोड़ने के लिए अवसर प्रदान करेगा।

ग्रीन आईएसटी टेबलव्यू हैडर, जबकि ब्लू सेक्शन हेडर दिखाता है।

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; 
} 

आप इस एप्लिकेशन को यहाँ के लिए कोड मिलेगा: lol MyContacts

+0

चारों ओर मैं देख रहा हूँ कि आप इसे का जवाब देने के बिना मेरे प्रश्न का उत्तर poking के बाद – vikingosegundo

+0

Welp मेरे संपादन और एक उदाहरण के लिए मेरे कोड देखें । एक बार फिर धन्यवाद! @vikingosegundo –

+0

@EricOboite: संकोच न करें और अगर यह मददगार हो तो वोट दें! – vikingosegundo

संबंधित मुद्दे