• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:06:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是我的 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": @"rofile"},
                           @{@"text": @"Matches"},
                           @{@"text": @"Messages"},
                           @{@"text": @"hotos"},
                           @{@"text": @"Settings"},
                           @{@"text": @"rivacy"},
                           @{@"text": @"Reporting issues"},
                           ];

}

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
 {

    return sectionTitleArray.count;
}

-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section

{
    return 1;
}

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 单元格中。任何帮助都是可观的。



Best Answer-推荐答案


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

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

关于ios - 如何在 UITableViewCell 中使用字典显示 NSArray 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34648874/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap