Pregunta

Estoy teniendo dificultades para raspar juntos suficientes fragmentos de conocimiento para implementar un NSOutlineView con una estática, estructura definida en un NSArray que nunca cambia. Este enlace ha sido grande, pero no me está ayudando submenús alcance. Estoy pensando que son simplemente NSArrays anidados, pero no tengo ninguna idea clara.

Vamos a decir que tenemos una NSArray dentro de un NSArray, definida como

NSArray *subarray = [[NSArray alloc] initWithObjects:@"2.1", @"2.2", @"2.3", @"2.4", @"2.5", nil];
NSArray *ovStructure = [[NSArray alloc] initWithObjects:@"1", subarray, @"3", nil];

El texto se define en outlineView: objectValueForTableColumn: byItem:.

- (id)outlineView:(NSOutlineView *)ov objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)ovItem
{
    if ([[[tableColumn headerCell] stringValue] compare:@"Key"] == NSOrderedSame)
    {
        // Return the key for this item. First, get the parent array or dictionary.
        // If the parent is nil, then that must be root, so we'll get the root
        // dictionary.

        id parentObject = [ov parentForItem:ovItem] ? [ov parentForItem:ovItem] : ovStructure;

    if ([parentObject isKindOfClass:[NSArray class]])
        {
            // Arrays don't have keys (usually), so we have to use a name
            // based on the index of the object.

        NSLog([NSString stringWithFormat:@"%@", ovItem]);
            //return [NSString stringWithFormat:@"Item %d", [parentObject indexOfObject:ovItem]];
        return (NSString *) [ovStructure objectAtIndex:[ovStructure indexOfObject:ovItem]];
        }
    }
    else
    {
        // Return the value for the key. If this is a string, just return that.

        if ([ovItem isKindOfClass:[NSString class]])
        {
            return ovItem;
        }
        else if ([ovItem isKindOfClass:[NSDictionary class]])
        {
            return [NSString stringWithFormat:@"%d items", [ovItem count]];
        }
        else if ([ovItem isKindOfClass:[NSArray class]])
        {
            return [NSString stringWithFormat:@"%d items", [ovItem count]];
        }
    }

    return nil;
}

El resultado es '1', '(' (expandible), y '3'. Shows NSLog la matriz empiezan con '(', por lo tanto el segundo elemento. La expansión provoca un accidente debido a ir 'más allá de los límites.' he intentado utilizar parentForItem: pero no podía entender por qué comparar el resultado a

.

¿Qué me falta?

¿Fue útil?

Solución

El ejemplo detrás de la relación que incluyó espectáculos NSDictionary un cuidado adecuado de la materia subarreglo, si estoy leyendo correctamente. Así que creo que su ovStructure no debe ser una matriz, pero un diccionario. Pero, más importante, creo que realmente debería mirar en NSTreeController. Por desgracia, NSTreeController es notoriamente difícil de trabajar, pero las mejoras fueron hechas el año pasado y aún lo tengo trabajando en el final. Buena suerte.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top