Перво-наперво, у вас есть утечка в коде, который вы представили.Удалите два вызова на retain
.
Во-вторых, у вас классическая проблема - иметь несколько цепочек switch / if..else, основанных на одной и той же информации.Это кричит для решения OO.
Сначала создайте класс TableSection:
@interface TableSection : NSObject
{ }
@property (nonatomic, copy) NSString* header;
@property (nonatomic, copy) NSArray* rows;
- (NSInteger)numberOfRows;
- (UITableViewCell*)cellInTableView: (UITableView*)tableView forRow: (NSInteger)row;
@end
@implementation TableSection
@synthesize header;
@synthesize rows;
- (void)dealloc {
[header release];
[rows release];
[super dealloc];
}
- (NSInteger)numberOfRows {
return rows.count;
}
- (UITableViewCell*)cellInTableView: (UITableView*)tableView forRow: (NSInteger)row {
// create/reuse, setup and return a UITableViewCell
}
@end
Теперь в вашем TableViewController
@interface MyViewController : UITableViewController
{ }
@property (nonatomic, retain) NSArray* tableSections;
@end
@implementation MyViewController
- (void)dealloc {
[tableSections release];
[super dealloc];
}
- (void)viewDidLoad {
TableSection* section1 = [[TableSection alloc] init];
[section1 setRows: [NSArray arrayWithObjects: @"Send SMS", @"Reports", nil]];
TableSectlion* section2 = [[TableSection alloc] init];
[section2 setRows: [NSArray arrayWithObjects: @"Accounts", nil]];
[self setTableSections: [NSArray arrayWithObjects: section1, section2, nil]];
[section2 release];
[section1 release];
}
- (void)viewDidUnload {
[self setTableSections: nil];
}
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView: (UITableView*)tableView {
return self.tableSections.count;
}
- (NSInteger)tableView: (UITableView*)tableView numberOfRowsInSection: (NSInteger)section {
return [[self.tableSections objectAtIndex: section] numberOfRows];
}
- (UITableViewCell*)tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath {
return [[self.tableSections objectAtIndex: indexPath.section] cellInTableView: tableView forRow: indexPath.row];
}
- (NSString*)tableView: (UITableView*)tableView titleForHeaderInSection: (NSInteger)section {
return [[self.tableSections objectAtIndex: section] header];
}
@end