ios - 填充分组的UITableView

标签 ios objective-c ios6 uitableview

我正在尝试使用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

当前是这样。

我如何以上述方式获得此信息? (第一部分:收件箱,第二部分:草稿,第三部分:已发送邮件等。)我已经很近了,但是还没到那儿。

谢谢。

最佳答案

您应该更改:

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


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

这些行应放在cellForRowAtIndexPath:方法中。

您的数组索引应基于分区而不是行。行始终固定为零。

关于ios - 填充分组的UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15288654/

相关文章:

ios - 将标签栏与 MVVM Light (iOS) 结合使用

objective-c - 在 iOS6 中处理通讯簿联系人时的内存泄漏

ios - Openudid 值不持久

c++ - 函数调用-Objective C

ios - 我的应用程序没有出现在 iOS6 的位置设置列表中

ios - 延迟加载 UIScrollView 或 UITableView

ios - NSOperation 获取图像并更新 UITableView UIImageView 属性

iphone - 通过 FB connect 获取好友事件时出现问题

ios - NSUserDefaults 拒绝存储 NSString 对象

iphone - 启动网站并登录 iOS