0

Я только что начал изучать Xcode, прочитал книгу и теперь пытаюсь реализовать то, что я прочитал. Приложение должно хранить определенные данные в массивах и словарях, тогда табличный вид извлекает данные, автоматически подсчитывая количество разделов, количество строк и заполняя ячейки таблицы содержимым, назначенным ключевому слову.TableView с NSMutableDictionary и массивами для заполнения ячеек

Пожалуйста, смотрите мой код, потому что я пытался запустить приложение, но это дает мне ошибку SIGABRT. Я не знаю, если новый Xcode не объявляет или не называет такие вещи (моя книга для iOS3). Я попытался найти решения из блогов, youtube, но приложение пока не работает. Я знаю, что я правильно описал файлы AppDelegate и т. Д., Потому что я использовал чей-то примерный код одного массива. И табличный вид заселен.

Помогите мне, потому что что-то не так с моим кодом с 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 

Я знаю, что я делаю что-то очень глупо с кодами, но я пытаюсь понять, как правильно писать коды ...

ответ

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

}

добавить этот код после initilization этих массивов и попробовать

Смежные вопросы