iPhone TableView数据分为2个部分

时间:2011-03-18 11:37:24

标签: iphone objective-c cocoa-touch ios ios4

我正在尝试使用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个部分,但填充行时只重复两个部分中的第一个内容数组。

任何人都可以提供帮助......

由于

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

不要在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];

答案 2 :(得分:0)

您需要确保(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;
     }
}

答案 3 :(得分:0)

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

相关问题