如何在 UITableViewCell 中使用字典显示 NSArray 对象

How to display NSArray objects using dictionary in UITableViewCell

这是我的viewcontroller.m

@interface ViewController ()

{

    NSArray *sectionTitleArray;
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];

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



    sectionTitleArray = @[ @{@"text": @"About Step0ne"},
                           @{@"text": @"Profile"},
                           @{@"text": @"Matches"},
                           @{@"text": @"Messages"},
                           @{@"text": @"Photos"},
                           @{@"text": @"Settings"},
                           @{@"text": @"Privacy"},
                           @{@"text": @"Reporting issues"},
                           ];

}

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

    return sectionTitleArray.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 = [[sectionTitleArray objectAtIndex:indexPath.row]objectForKey:@"text"];

        return cell;
}

我是新手 Xcode 我正在获取输出,因为在每个 tableview 单元格中第一个对象,即关于 Step0ne 正在显示我需要所有对象都应该显示在 UITableView cell.Any 帮助是可观的。

由于您有 "count" 个部分,每个部分一行,您需要更改此行:

cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.row]objectForKey:@"text"];

至:

cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.section]objectForKey:@"text"];

顺便说一句 - 这可以更容易地写成:

cell.textLabel.text = sectionTitleArray[indexPath.section][@"text"];

或者,如果您真的想要一个包含 "count" 行的部分,请保留该行 as-is 并相应地更新您的 numberOfRowsInSectionnumberOfSectionsInTableView

替换下面两种方法

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
return sectionTitleArray.count;

}