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

How to display two NSArray objects as sections of UITableview using NSDictionary in ios

这是我的代码 ViewController.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的两个部分。而 stepone1stepone2、....stepone8 应该是我的关于 Stepone 部分的行。

要向 Table 视图添加多个部分,请使用委托方法 numberOfSectionsInTableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return sectionTitleArray.count;
}

要显示特定行数,请使用此委托:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dict = [sectionTitleArray ObjectAtIndex:section]
    NSString *strSection = [[dict allKeys] objectAtIndex:2];
    NSArray *rows= [dict ObjectForKey:strSection];
    return rows.count;
}

我想你错过了这行代码..

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

您可以通过以下代码使用它:

对于部分编号,您必须在 numberOfSectionsInTableView 委托方法中 return 数组计数。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return sectionTitleArray.count;// Total number of sections in the tableview.
}

实现以下委托方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return sectionTitleArray.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *dict=[sectionTitleArray objectAtIndex:section];

    return dict[@"description"];
}

您需要return下面的行数不是1

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dict=[sectionTitleArray objectAtIndex:section];

    NSArray *array=dict[dict[@"description"]];

    return array.count;
}

试试这个...

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
          return 2 ;
     }

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
          if (section==0)
          {
                 return [array1 count];
          }
          else{
                 return [array2 count];
          }
     }

     - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
          if(section == 0)
               return @"Section 1";
          else
               return @"Section 2";
     }


     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {

          static NSString *CellIdentifier = @"Cell";

          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
          }

     if (indexPath.section==0) {
         ObjectData *theCellData = [array1 objectAtIndex:indexPath.row];
         NSString *cellValue =theCellData.category;
         cell.textLabel.text = cellValue;
     }
     else {
         ObjectData *theCellData = [array2 objectAtIndex:indexPath.row];
         NSString *cellValue =theCellData.category;
         cell.textLabel.text = cellValue;
     }
         return cell;    
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return sectionTitleArray.count;
}

--before your returning change both key names as same.
like for exampel profile and stepone should be same key

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[sectionTitleArray objectAtIndex:section]valueForKey:@"About Stepone"]count];
}

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

{
    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];
}

编码愉快。

输出如下: