Чтение plist в TableView - PullRequest
       15

Чтение plist в TableView

0 голосов
/ 10 декабря 2010

Я начал этот проект с простого списка словаря с двумя массивами строк. Теперь я хочу добавить больше информации и использовать следующую структуру:

Root - Dictionary - (2 items)
   Standard - Array - (3 items)
      Item 0 - Dictionary - (4 items)
           Color - String - Red
           Rvalue - String - 255
           Gvalue - String - 0
           Bvalue - String - 0

Извините, что набрал в плисте, но сайт не позволил мне опубликовать изображение

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

Это код, который я использовал для чтения простого списка:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    NSString *key = [keys objectAtIndex:section];
    NSArray *colorSection = [colors objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

    if(cell == nil){
        cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: SectionsTableIdentifier] autorelease];
        }

    cell.textLabel.text = [colorSection objectAtIndex:row];
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; //add disclosure button to rows
    return cell;
}

Мой вопрос заключается в том, какой конкретный код используется для получения содержимого словарей цветов, чтобы получить цвета для cell.textLabel.text, а также для чтения значений RGB, чтобы добавить субтитры. Я работал над этим в течение нескольких дней и прочитал ссылки и множество примеров, и, к сожалению, не могу решить проблему. Ваша помощь будет принята с благодарностью.

Ответы [ 2 ]

1 голос
/ 19 декабря 2010

Во-первых, не используйте - [UITableViewCell initWithFrame: reuseIdentifier:]. Он устарел и выдаст вам предупреждение, а также затруднит реализацию ваших субтитров. Этот код является вашей измененной версией, которая загружает информацию, устанавливает заголовок для свойства Color и устанавливает для субтитров строку, содержащую свойства Rvalue, Gvalue и Bvalue.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    NSString *key = [keys objectAtIndex:section];
    NSArray *colorSection = [colors objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

    if(cell == nil) {
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: SectionsTableIdentifier] autorelease];
    }

    NSDictionary *color = [colorSection objectAtIndex:row];
    cell.textLabel.text = [color objectForKey:@"Color"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@, %@",[color objectForKey:@"Rvalue"],[color objectForKey:@"Gvalue"],[color objectForKey:@"Bvalue"]];
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; //add disclosure button to rows
    return cell;
}
1 голос
/ 10 декабря 2010

Таким образом, если у вас есть стандартный массив, хранящийся в массиве, который вы определили в своем файле .h, тогда будет работать нечто подобное. В этом примере массив сохраняется в self.coloursArray.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

if(cell == nil){
    cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: SectionsTableIdentifier] autorelease];
    }
NSString* ColourString = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Colour"];
NSString* rValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Rvalue"];
NSString* gValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Gvalue"];
NSString* bValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Bvalue"];
cell.textLabel.text = ColourString;
NSString* subCellString = [NSString stringWithFormat:@"%@:%@:%@", rValue, gValue, bValue];
}

Надеюсь, это поможет.

...