2011-03-18 52 views
0

我想獲得一個實現代碼如下拆分我有一個數組段數據...iPhone的TableView數據到2節

我很新的這一點,但下面是我的代碼片段

- (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個部分,但填充行時它只是重複兩個部分中的第一個內容數組。

任何一個可以幫助...

感謝

+0

你可以發佈代碼cellForRowAtIndexPath – visakh7 2011-03-18 11:46:50

+0

在你的示例代碼中,你將有很多內存泄漏。例如,最好在viewDidLoad中創建數組。因爲您需要通過幾種方法訪問數組。查看@fluchtpunkt的答案。 – 2011-03-18 12:32:32

回答

0

你需要確保根據您的內容陣列的分裂(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法也返回正確的細胞。您可以在的tableview中的數據源或委託的方法之一做的方法東西類似的檢查一樣

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

不切換(和泄漏)的內容數據。 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

第一件事,你的代碼泄漏了。刪除兩個電話retain

其次,您在基於相同信息的多個交換機/ if..else鏈中存在經典問題。這是一個面向對象解決方案的尖叫。

首先創建一個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 
0

我想你應該把代碼中的cellForRowAtIndexPath,因爲你在哪裏書面方式有我們一般添加的行數在每個部分