TableView 带有 NSMutableDictionary 和用于填充单元格的数组

发布于 2025-01-03 07:30:21 字数 3501 浏览 2 评论 0原文

我刚刚开始学习 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

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

I just started learning Xcode, read the book and now trying to implement what I read. The app needs to store certain data in arrays and dictionary, then table view extracts the data by automatically counting number of sections, number of rows and populating table cells with content, assigned to a key word.

Please, see my code, because I've been trying to run the app, but it gives me SIGABRT error. I don't know if new Xcode doesn't declare or call things this way(my book is for iOS3). I tried to find solutions from blogs, youtube, but the app is still not running at this point.
I know that I declared everything correctly in AppDelegate files, etc, because I used someone's sample code of one 1 array. And the table view got populated.

Help me, because something is wrong with my code with NSMutableDictionary and indexPath. Thank you!

#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

I know I'm doing something very silly with the codes, but I'm trying to understand the right way to write the codes...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○闲身 2025-01-10 07:30:21

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

尝试

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

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

}

add this code after initilization of those arrays and try

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文