ios - 如何在 ios 中使用 NSDictionary 将两个 NSArray 对象显示为 UITableview 的部分

标签 ios objective-c uitableview nsarray nsdictionary

<分区>

这是我的代码 View Controller .m

{
    NSArray *sectionTitleArray;

}
@property (strong, nonatomic) IBOutlet UITableView *tablevw;

- (void)viewDidLoad
{
    [super viewDidLoad];

    _tablevw.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    _tablevw.delegate = self;
    _tablevw.dataSource = self;

    sectionTitleArray = @[ @{ @"description": @"About Step0ne",
                              @"About Stepone": @[ @{ @"text": @"Step0ne 1" },
                                              @{ @"text": @"Step0ne 2" },
                                              @{ @"text": @"Step0ne 3" },
                                              @{ @"text": @"Step0ne 4" },
                                              @{ @"text": @"Step0ne 5" },
                                              @{ @"text": @"Step0ne 6" },
                                              @{ @"text": @"Step0ne 7" },
                                              @{ @"text": @"Step0ne 8" },
                                              ]
                              },
                           @{ @"description": @"Profile",
                              @"profile": @[ @{ @"text": @"profile 1" },
                                              @{ @"text": @"profile 2" },
                                              @{ @"text": @"profile 3" },
                                              @{ @"text": @"profile 4" },
                                              @{ @"text": @"profile 5" },
                                              @{ @"text": @"profile 6" },
                                              @{ @"text": @"profile 7" },
                                              @{ @"text": @"profile 8" },
                                              ]
                              },

                              ];
}

-(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 = [[sectionTitleArray objectAtIndex:indexPath.section]objectForKey:@"About Stepone"];
    cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.section]objectForKey:@"Profile"];

    return cell;
}

About SteponeProfile 是我的tableview 的两个部分。而 stepone1,stepone2,....stepone8 应该是我的关于 Stepone 部分的行。

最佳答案

这里有一些经过验证的代码供您使用。 为了方便起见,我对您的数据结构做了一些更改。希望你觉得这有帮助。

{
    NSDictionary *_objects;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _objects = @{@"About Stepone": @[ @{ @"text": @"Step0ne 1" },
                                      @{ @"text": @"Step0ne 2" },
                                      @{ @"text": @"Step0ne 3" },
                                      @{ @"text": @"Step0ne 4" },
                                      @{ @"text": @"Step0ne 5" },
                                      @{ @"text": @"Step0ne 6" },
                                      @{ @"text": @"Step0ne 7" },
                                      @{ @"text": @"Step0ne 8" }],
                 @"profile": @[ @{ @"text": @"profile 1" },
                                @{ @"text": @"profile 2" },
                                @{ @"text": @"profile 3" },
                                @{ @"text": @"profile 4" },
                                @{ @"text": @"profile 5" },
                                @{ @"text": @"profile 6" },
                                @{ @"text": @"profile 7" },
                                @{ @"text": @"profile 8" }]
                 };

}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table View

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ((NSArray *) _objects[_objects.allKeys[section]]).count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDictionary *object = _objects[_objects.allKeys[indexPath.section]][indexPath.row];
    cell.textLabel.text = object[@"text"];
    return cell;
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return _objects.allKeys[section];
}

快乐编码。

输出如下:

Output of above lines

关于ios - 如何在 ios 中使用 NSDictionary 将两个 NSArray 对象显示为 UITableview 的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34653723/

上一篇:ios - IBAction 链接到多个 UISliders

下一篇:ios - 在 UIPageViewController 上没有弹跳?

相关文章:

objective-c - OS X 上的 3D CAD 图形框架 - 有建议吗?

ios - 最喜欢的按钮 : pass cell

objective-c - 在部分之间移动 uitableviewcells 时添加长按手势

ios - 如何从 NSDictionary 获取键/值对?

iOS 6 SLComposeViewController - 创建带有文本和 URL 的 facebook 帖子 - 没有图像

ios - Objective-C 尝试为 NSArray 中的每个项目创建标签 - 不起作用

iphone - 在 UIWebView 中加载大网页

ios - 根据应用的过滤器过滤 UITableView 的内容

ios - 如何从 MPMoviePlayerViewController 中删除 "Loading..."标签?

ios - 从自定义快捷操作中传递参数