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

时间:2016-01-07 06:44:04

标签: ios objective-c uitableview nsarray nsdictionary

这是我的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单元格中。任何帮助都是值得的。

2 个答案:

答案 0 :(得分:4)

由于您有“计数”部分,每个部分都有一行,因此您需要更改此行:

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"];

或者,如果您确实想要一个包含“计数”行的部分,请保持该行不变,并相应地更新numberOfRowsInSectionnumberOfSectionsInTableView

答案 1 :(得分:-1)

替换以下两种方法

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

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

{
return sectionTitleArray.count;

}