/ / Cree un UITableView seccionado usando NSMutableArray of NSDictionaries - ios, object-c, uitableview, nsmutablearray, nsdictionary

Cree un UITableView seccionado utilizando NSMutableArray of NSDictionaries - ios, object-c, uitableview, nsmutablearray, nsdictionary

Yo tengo un NSMutableArray de NSDictionaries que estoy tratando de formatear para su uso con una UITableView que tiene secciones basadas en una de las NSDictionary keys.

Mi NSDicitonary tiene dos valores

Name
Key

La clave es un NSString de los números 1 - N

He logrado crear un NSArray de valores únicos que estoy usando para rellenar los encabezados de cada sección, sin embargo no sé cómo dividir esto NSMutableArray de NSDictionariesinto anNSMutableArray of NSArrays of NSDictionaries` .. si eso tiene sentido.

ACTUALIZAR: Para agregar más detalles a mi pregunta, tengo un NSMutableArray de NSDictionaries.

{
Name: Jack
Key: 1
}
{
Name: John
Key: 1
}
{
Name: Jack
Key: 1
}
{
Name: Jack
Key: 2
}
{
Name: Jack
Key: 3
}
{
Name: Sean
Key: 3
}
{
Name: Sally
Key: 3
}

y me gustaría mostrarlo en un UITableView como este

Section 1
- Jack
- John
- Jack
Section 2
- Jack
Section 3
- Jack
- Sean
- Sally

Las áreas en las que he identificado que necesito hacer las cosas son

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

He logrado crear un únicoArray para secciones

uniqueKeys = [xmlMutableArray valueForKeyPath:@"@distinctUnionOfObjects.Key"];
uniqueKeys = [uniqueKeys sortedArrayUsingComparator:^(id obj1, id obj2) {
return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];

Ahora me quedo en la situación de cómo encontrar el número de filas en cada sección y luego pasar el NSDictionary a un objeto en cellforrow para mostrar los valores correctos en la celda.

Cualquier ayuda sería apreciada.

Respuestas

0 para la respuesta № 1

Espero que esto te ayudará:

// testing
NSMutableArray *test = [[NSMutableArray alloc]init];
NSArray *testSample = @[@{@"1": @"hello1",@"2":@"world1",@"3":@"!1"},
@{@"1": @"hello2",@"2":@"world2",@"3":@"!2"},
@{@"1": @"hello3",@"2":@"world3",@"3":@"!3"}];
[test addObjectsFromArray:testSample];
NSMutableArray *sortedTestArray = [[NSMutableArray alloc] init];
NSLog(@"%@",test);

//get the max length of the nsdictionary
NSDictionary *item0 = [test objectAtIndex:0];
NSInteger maxLength = [[item0.allKeys lastObject] integerValue];
//key start from @"1"
for (int a = 1; a <= maxLength; a++) {
NSString *keyNumber = [NSString stringWithFormat:@"%d", a];
NSArray *sortedItem = [test valueForKey:keyNumber];
//the index starts from 0
[sortedTestArray insertObject:sortedItem atIndex:a-1];
}
//now the array will be like this,so the item in index 0 will be the key @"1""s items. index 1 will be the key @"2""s items...
NSLog(@"%@",sortedTestArray);

sus datos originales:

(
{
1 = hello1;
2 = world1;
3 = "!1";
},
{
1 = hello2;
2 = world2;
3 = "!2";
},
{
1 = hello3;
2 = world3;
3 = "!3";
}
)

salida después de ordenado:

(
(
hello1,
hello2,
hello3
),
(
world1,
world2,
world3
),
(
"!1",
"!2",
"!3"
)
)