2011-10-06 19 views
13

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

मेरा कोड यहां है।

TableViewController.h

#import <UIKit/UIKit.h> 
@interface TableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{ 
    NSMutableArray *tableDataSource;  
} 
@end 

TableViewController.m

#import "FourthViewController.h" 
@implementation TableViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)dealloc{ 
    [super dealloc]; 
    [tableDataSource release]; 
} 

- (void)didReceiveMemoryWarning{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStyleGrouped]; 
    [table setDataSource:self]; 
    [table setDelegate:self]; 
    [table release]; 

    tableDataSource = [[NSMutableArray alloc]init]; 

    NSMutableArray* sec1 = [[NSMutableArray alloc] init]; 
    [sec1 addObject:@"1"]; 
    [sec1 addObject:@"2"]; 
    [sec1 addObject:@"3"]; 

    [tableDataSource addObject:sec1]; 
    [sec1 release]; 

    NSMutableArray* sec2 = [[NSMutableArray alloc] init]; 
    [sec2 addObject:@"One"]; 
    [sec2 addObject:@"Two"]; 
    [sec2 addObject:@"Three"]; 

    [tableDataSource addObject:sec2]; 
    [sec2 release]; 

    [self.view addSubview:table]; 

} 

- (void)viewDidUnload{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tableDataSource == nil) 
     return 1; 
    return [tableDataSource count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
    NSInteger bucketCount = -1; 
    NSObject *target_section; 
    if (tableDataSource == nil) 
     return 0; 
    if((bucketCount = [tableDataSource count]) < 1 || bucketCount <= section || (target_section = [tableDataSource objectAtIndex:section ]) == nil) 
     return 0; 
    return [ (NSMutableArray*)target_section count ]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]]; 
    if (cell == nil) 
     { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease]; 
     } 

cell.textLabel.text = (NSString*)[ (NSMutableArray*)[tableDataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
@end 

उत्तर

23
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if(section == 0) 
    { 
     return @"Title0"; 
    } 
    else if(section == 1) 
    { 
     return @"Title1"; 
    } 
    else 
    { 
     return @"Title2"; 
    } 
} 
+0

धन्यवाद जाँच के रूप में तालिका दृश्य की धारा मिल जाएगा। क्या होगा अगर वर्गों की संख्या दो से अधिक है? – lavitanien

+2

ब्लॉक अगर और भी जोड़ें। या स्विच-केस का उपयोग करें। – Akshay

5

इस प्रयास करें:

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"title1", 
                   @"title2", 
                   @"title3", 
                   @"title4", 
                   nil]; 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    return [array objectAtIndex:section]; 
} 
0

आप कॉल कर सकते हैं तालिका दृश्य डेटा स्रोत विधि

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    return @"title for header"; 
} 

यहाँ आप से है कि आप अपने वर्तमान अनुभाग

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    if(section == 0) 
     { 
     return @"Section 0 title"; 
     } 
    else if (section == 1){ 
     return @"Section 1 title"; 
     } 
    return @"title for header"; 
} 
+1

क्या यह कुछ नया जोड़ता है? क्योंकि यह मूल रूप से वही उत्तर है जो पहले ही स्वीकार कर लिया गया है। –