【问题标题】:How to display NSArray objects using dictionary in UITableViewCell如何在 UITableViewCell 中使用字典显示 NSArray 对象
【发布时间】:2016-01-07 06:44:04
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c uitableview nsarray nsdictionary


    【解决方案1】:

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

    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

    【讨论】:

    • 如果我需要 8 个部分,我应该如何修改上面的代码。
    • 奇怪的是,您似乎在接受答案后发表了此评论,所以我不确定您为什么要问。我的答案的前半部分解释了当你想要每节一行时该怎么做。
    • 现在我有另一个与此相关的要求,您只需稍作修改即可指导我。
    • 我有 8 个数组,而不是一个数组。我应该如何在 tableview 中显示
    • 您应该发布一个包含相关详细信息的新问题。
    【解决方案2】:

    替换下面两种方法

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

    【讨论】:

      猜你喜欢
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多