iPhone TableView Data Into 2 Sections
I am trying to get a tableview to split data i have in an array into sections...
I'm pretty new to this but below is my code snippet
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
开发者_如何学C contentArray = [[NSArray arrayWithObjects:@"Send SMS", @"Reports", nil] retain];
}
if(section == 1)
{
contentArray = [[NSArray arrayWithObjects:@"Accounts", nil] retain];
}
return [contentArray count];
}
I have split the data successfully into the 2 sections but when populating the rows It just repeats the first content array in both sections.
Can any one help...
Thanks
First things first, you have a leak in the code you presented. Remove the two calls to retain
.
Second, you are in the classic problem of having multiple switches/if..else chains based on the same information. This screams for an OO solution.
First create a TableSection class:
@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
Now in your 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
don't switch (and leak) your content data in one of the tableview datasource or delegate methods. tableView:numberOfRowsInSection:
can be called an arbitrary number of times. With your code you are relying on a particular order in which those methods are called.
You should have a separate array for each section (or one array that holds the two section arrays)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0) {
return [section0Array count];
}
if(section == 1) {
return [section1Array count];
}
return 0;
}
And you could create those arrays in viewDidLoad:
section0Array = [[NSArray arrayWithObjects:@"Send SMS", @"Reports", nil] retain];
section1Array = [[NSArray arrayWithObjects:@"Accounts", nil] retain];
i suppose you should put the code in cellForRowAtIndexPath because where you are writting there we generally add the count of rows in each section
You need to make sure that the (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method is also returning the correct cells based on your splitting of the content array. You can do the similar check in the method something like
switch(indexPath.section)
{
case 0: //First Section
switch(indexPath.row)
{
case 0: //Setup the cell Send SMS
break;
case 1: //Setup the cell Reports
break;
}
}
精彩评论