2013-03-08 102 views
1

我試圖使用NSMutableArray填充分組UITableView。我希望數組中的每個元素都有自己的部分。即:每個部分一個元素(一行)。填充分組的UITableView

這是我寫到目前爲止的代碼。

#import "MailViewController.h" 

@interface MailViewController() 

@end 

@implementation MailViewController 

NSMutableArray *mailboxes; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 

    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    mailboxes = [[NSMutableArray alloc] initWithObjects:@"Inbox", @"Drafts", @"Sent Items", nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return mailboxes.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row]; 

    return cell; 
} 

@end 

目前這是它的樣子。

enter image description here

我怎樣才能得到這個我上面描述的方式? (第一部分:收件箱,第二部分:草稿,第三部分:發送物品等)我已經接近但尚未完成。

謝謝。

+0

上面的截圖描繪了,你想要不要段,你是在談論節嗎?因爲章節確實有他們自己的委託方法來定義他們的標題。 – 2013-03-08 07:15:43

回答

5

你應該改變:

cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row]; 

cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section]; 

這些線應該在cellForRowAtIndexPath:方法。你的數組索引應該基於段而不是行。行總是固定爲零。

1

更換

cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row]; 

隨着

cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section]; 
0

如果你想副標題落實好- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section和返回基於部分字符串..

與其他答案代碼,您將能夠在每個部分內顯示一個單元格。我喜歡做的是預先建立的單元格的多維NSArray,當我需要它們時,它們在索引[section][row]中。

你目前只有一個數組只有部分,並試圖訪問它們作爲行(如其他答案建議)。如果你在一個表組中有更多的郵箱,你的代碼將無法工作。

-1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [mailboxes count]; 
} 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section 
    { 
    return 1; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier]; 
    } 
     switch(indexpath.section){ 
      case 0: 
       cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section]; 
       case 1: 
       cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section]; 
       case 2: 
       cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section]; 
       default: 
       break; 
      } 
    return cell; 
} 
+0

這裏不需要使用switch語句。 – 2013-12-22 22:38:33