使用 NSMutableDictionary 和数组填充单元格的 TableView

     2023-02-27     276

关键词:

【中文标题】使用 NSMutableDictionary 和数组填充单元格的 TableView【英文标题】:TableView with NSMutableDictionary and arrays to fill in the cells 【发布时间】:2012-02-08 06:32:21 【问题描述】:

我刚开始学习 Xcode,阅读了这本书,现在尝试实现我阅读的内容。该应用程序需要将某些数据存储在数组和字典中,然后表格视图通过自动计算节数、行数和使用分配给关键字的内容填充表格单元格来提取数据。

请查看我的代码,因为我一直在尝试运行该应用程序,但它给了我 SIGABRT 错误。我不知道新的 Xcode 是否没有以这种方式声明或调用事物(我的书适用于 iOS3)。我试图从博客、youtube 中找到解决方案,但该应用程序此时仍未运行。 我知道我在 AppDelegate 文件等中正确声明了所有内容,因为我使用了某人的一个 1 数组的示例代码。并且表格视图被填充了。

帮助我,因为我的 NSMutableDictionary 和 indexPath 代码有问题。谢谢!

#import "TableViewController.h"


@implementation TableViewController
@synthesize tableData;
@synthesize tableSections;

#pragma mark - View lifecycle

- (void)viewDidLoad
 [self createData];
[super viewDidLoad];

-(void)createData

NSMutableArray *category1;
NSMutableArray *category2;

category1=[[NSMutableArray alloc] init];
category2=[[NSMutableArray alloc] init];

  tableSections=[[NSMutableArray alloc] initWithObjects:@"Category 1", @"Category 2", nil];   

tableData=[[NSMutableArray alloc] initWithObjects:category1, category2, nil];


[category1 addObject:[[NSMutableDictionary alloc]
                               initWithObjectsAndKeys:@"TitleA", @"name",@"A",@"property", nil]];
[category1 addObject:[[NSMutableDictionary alloc]
                               initWithObjectsAndKeys:@"TitleB", @"name",@"B",@"property", nil]];

[category2 addObject:[[NSMutableDictionary alloc]
                               initWithObjectsAndKeys:@"TitleC", @"name",@"C",@"property", nil]];

[category2 addObject:[[NSMutableDictionary alloc]
                               initWithObjectsAndKeys:@"TitleD", @"name",@"D",@"property", nil]];





#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

return [tableSections count];
//return [self.tableSections count];      Also tried to use with self. Don’t know which one is correct


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    
return [[self.tableSections objectAtIndex:section] count]; 


- (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 = [[[tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];


/* Also tried this:
tableData *cellObj = [[self.tableData objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
cell.textLabel.text = cellObj.name;   

/* Also tried this:
tableData *cellObj= [[[self.tableData objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.textLabel.text=smsObj.name;   */


/* Also tried this:
NSDictionary *item = (NSDictionary *)[self.tableData objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"name"];*/


/* Also tried this way:
NSDictionary *dictionary = [tableData objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"name"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.text = cellValue;    

And the same way, but switched order of NSArray and NSDictionary*/



return cell;



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

// This I will add later, after I make my Table View Controller work


@end

我知道我对代码做了一些非常愚蠢的事情,但我正在尝试了解编写代码的正确方法...

【问题讨论】:

【参考方案1】:

[category1 addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"TitleA", @"name",@"A",@"property", nil]]; [category1 addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"TitleB", @"name",@"B",@"property", nil]];

[category2 addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"TitleC", @"name",@"C",@"property", nil]];

[category2 addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"TitleD", @"name",@"D",@"property", nil]];

在这些数组初始化后添加此代码并尝试

【讨论】:

如何使用 NSMutableDictionary 导出 .JSON 文件?

】如何使用NSMutableDictionary导出.JSON文件?【英文标题】:Howtoexport.JSONfileusingNSMutableDictionary?【发布时间】:2016-05-1314:36:42【问题描述】:我有NSMutableDictionary里面这个值是这样存在的0=imageangle="0.09630692";imageheight="129.0245";imagepath="as... 查看详情

使用 NSMutableDictionary 将对象保存到数组

】使用NSMutableDictionary将对象保存到数组【英文标题】:SavinganobjecttoarrayusingNSMutableDictionary【发布时间】:2011-10-0620:37:03【问题描述】:我一直在尝试将一个对象作为NSMutableDictionary添加到我从另一个视图访问的数组中,但它似乎... 查看详情

释放数组时 NSMutableDictionary 中使用的对象的内存会发生啥?

】释放数组时NSMutableDictionary中使用的对象的内存会发生啥?【英文标题】:whathappendswiththememoryoftheobjectsusedinNSMutableDictionarywhenreleasingthearray?释放数组时NSMutableDictionary中使用的对象的内存会发生什么?【发布时间】:2011-09-2122:05:... 查看详情

使用 UIImageView 作为 NSMutableDictionary 中的键来查找布尔值

】使用UIImageView作为NSMutableDictionary中的键来查找布尔值【英文标题】:UsingUIImageViewaskeyinNSMutableDictionarytolookupboolean【发布时间】:2013-02-1721:26:39【问题描述】:标题几乎说明了一切。我想创建一个NSMutableDictionary,在其中使用UIIma... 查看详情

使用指向链接列表的指针存储 NSMutableDictionary 一个 C 结构

】使用指向链接列表的指针存储NSMutableDictionary一个C结构【英文标题】:StoringNSMutableDictionaryaCstructwithapointertoalinklist【发布时间】:2012-09-1803:25:10【问题描述】:我有这个C结构(使用malloc分配,因此在堆中)我想存储在NSMutableDic... 查看详情

swift : 使用 NSPredicate 过滤 NSMutableDictionary 的 NSMutableArray

】swift:使用NSPredicate过滤NSMutableDictionary的NSMutableArray【英文标题】:swift:UseNSPredicatetofilterNSMutableArrayofNSMutableDictionary【发布时间】:2015-12-1704:59:40【问题描述】:以下是字典数组:DATA:[GAMEID:509782,FIELDNUMBER:"KPB#1",,GAMEID:509782 查看详情

如何使用 NSMutableDictionary 连接 Tableview 和 View Controller?

】如何使用NSMutableDictionary连接Tableview和ViewController?【英文标题】:HowtoConnectTableviewandViewControllerUsingNSMutableDictionary?【发布时间】:2014-02-0309:12:31【问题描述】:在我的iphone设计中,添加了两个表格视图。一个表格视图用于显示... 查看详情

使用 nsmutabledictionary 创建嵌套的 JSON 字符串

】使用nsmutabledictionary创建嵌套的JSON字符串【英文标题】:CreatingnestedJSONstringusingnsmutabledictionary【发布时间】:2013-10-2703:47:30【问题描述】:我正在尝试创建以下格式的json字符串:"cat_id":4992,"brand":"Toshiba","weight":"gte":1000000,"lt":150... 查看详情

NSDictionary 和 NSMutableDictionary 组合

】NSDictionary和NSMutableDictionary组合【英文标题】:NSDictionaryandNSMutableDictionarycombination【发布时间】:2016-03-1104:54:31【问题描述】:我的应用程序使用存储在dataModel类中的deviceToken类。设备令牌通过各种AFNetworkingcommands在整个应用程... 查看详情

使用 NSMutableDictionary 和数组填充单元格的 TableView

】使用NSMutableDictionary和数组填充单元格的TableView【英文标题】:TableViewwithNSMutableDictionaryandarraystofillinthecells【发布时间】:2012-02-0806:32:21【问题描述】:我刚开始学习Xcode,阅读了这本书,现在尝试实现我阅读的内容。该应用程... 查看详情

使用 NSJSONSerialization 将 NSMutableDictionary 转换为 JSON 会返回不同的结果

】使用NSJSONSerialization将NSMutableDictionary转换为JSON会返回不同的结果【英文标题】:ConvertingNSMutableDictionarytoJSONwithNSJSONSerializationreturnsdifferentresults【发布时间】:2013-08-0120:06:49【问题描述】:当我尝试使用NSJSONSerialization将NSMutableDi... 查看详情

如何使用键将对象添加到 NSMutableDictionary?

】如何使用键将对象添加到NSMutableDictionary?【英文标题】:HowdoIaddobjecttoNSMutableDictionarywithkey?【发布时间】:2013-05-2006:48:19【问题描述】:我有一个列表。我想将该plist中的元素添加到可变字典中。首先我检查plist中的用户名,... 查看详情

NSMutableDictionary 没有得到预期的输出。

】NSMutableDictionary没有得到预期的输出。【英文标题】:NSMutableDictionarynotgettingexpectedoutput.【发布时间】:2011-12-0717:08:14【问题描述】:它只为NSMutableDictionary输出一组,而不是两者。我想使用NSMutableDictionary(JSONRepresentation)创建一... 查看详情

无法更新 NSMutableDictionary?

】无法更新NSMutableDictionary?【英文标题】:CannotupdateNSMutableDictionary?【发布时间】:2016-02-0320:44:31【问题描述】:我不明白为什么我在使用此方法时会出错:letloginCountKey="loginCount"letappReviewParamsKey="appReviewParams"funcresetLoginCount()letd... 查看详情

JSON 的 NSMutableDictionary

】JSON的NSMutableDictionary【英文标题】:NSMutableDictionaryforJSON【发布时间】:2012-05-2315:12:53【问题描述】:我遇到了一个问题!我有一个带有JSON的字符串,例如:"用户名":"用户名","密码":"密码"我使用xcode构建这个JSON字符串,例如... 查看详情

nsmutabledictionary怎么排序

参考技术A排序NSMutableDictionary中元素:放在NSMutableDictionary中的元素,进行遍历的顺序和存入的时候的顺序是不一定相同的。所以,要进行排序输出。比如按照薪水的从低到高进行排序输出:-(NSArray*)sortBySalaryForDictionaryNSArray*sortedK... 查看详情

AFNetworking - NSMutableDictionary 参数

】AFNetworking-NSMutableDictionary参数【英文标题】:AFNetworking-NSMutableDictionaryparameters【发布时间】:2017-02-1703:38:14【问题描述】:我正在尝试构建我的NSMutableDictionary以使用AFNetworking框架发送到请求,但我似乎对如何正确执行此操作感... 查看详情

字典nsdictionary和nsmutabledictionary的使用(代码片段)

简介:字典是一种数据结构,字典里面的每一个元素,是一个key-value(键值对),key和value都是对象类型。同NSArray一样,里面的对象不用保持一致性。NSDictionary1、字面量初始化//存储时时无序的NSDictionary*dict=@@"name":@"xiaoming",@"gend... 查看详情