iPhone TableView数据分为2个部分

我试图得到一个tableview将我在数组中的数据分成几个部分……

我对此很新,但下面是我的代码片段

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section == 0) { contentArray = [[NSArray arrayWithObjects:@"Send SMS", @"Reports", nil] retain]; } if(section == 1) { contentArray = [[NSArray arrayWithObjects:@"Accounts", nil] retain]; } return [contentArray count]; } 

我已成功将数据拆分为2个部分,但填充行时只重复两个部分中的第一个内容数组。

任何人都可以帮忙……

谢谢

首先,您提供的代码中存在泄漏。 删除两个retain调用。

其次,您遇到了基于相同信息的多个交换机/ 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 

不要在tableview数据源或委托方法之一中切换(和泄漏)内容数据。 tableView:numberOfRowsInSection:可以被调用任意次数。 使用您的代码,您依赖于调用这些方法的特定顺序。

每个部分应该有一个单独的数组(或者一个包含两个部分数组的数组)

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section == 0) { return [section0Array count]; } if(section == 1) { return [section1Array count]; } return 0; } 

你可以在viewDidLoad中创建这些数组:

 section0Array = [[NSArray arrayWithObjects:@"Send SMS", @"Reports", nil] retain]; section1Array = [[NSArray arrayWithObjects:@"Accounts", nil] retain]; 

我想你应该把代码放在cellForRowAtIndexPath中,因为你在那里写的我们通常在每个部分添加行数

您需要确保(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法还根据您对内容数组的拆分返回正确的单元格。 您可以在方法中执行类似的检查

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