填充分组的UITableView

Isu*_*uru 1 objective-c uitableview ios ios6

我正在尝试UITableView使用NSMutableArray.填充分组.我希望数组中的每个元素都有自己的部分.即:每个部分一个元素(一行).

这是我到目前为止编写的代码.

#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
Run Code Online (Sandbox Code Playgroud)

目前这是它的外观.

在此输入图像描述

我怎么能按照上面描述的方式得到这个?(第一部分:收件箱,第二部分:草稿,第三部分:已发送物品等)我已经接近但尚未完成.

谢谢.

iDe*_*Dev 5

你应该改变:

cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)

cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
Run Code Online (Sandbox Code Playgroud)

这些行应该放在cellForRowAtIndexPath:方法中.

您的数组索引应该基于节而不是行.行始终固定为零.