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

时间:2012-02-08 06:32:21

标签: uitableview nsmutablearray nsmutabledictionary nsindexpath

我刚刚开始学习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 个答案:

答案 0 :(得分:0)

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

}

在启动这些数组后添加此代码并尝试