Reading plist into TableView

后端 未结 2 1625
轮回少年
轮回少年 2021-02-11 10:37

I started this project with a simple plist of a dictionary with two arrays of strings. I now want to add more information and want to use this plist structure:

R         


        
相关标签:
2条回答
  • 2021-02-11 11:15

    So providing you have your Standard - Array stored against a Array you've defined in your .h file then something like this would work. In this example the array is stored against 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];
    }
    

    Hopefully that'll give a a hand.

    0 讨论(0)
  • 2021-02-11 11:18

    First, do not use -[UITableViewCell initWithFrame:reuseIdentifier:]. It has been deprecated and will give you a warning, plus it will make your subtitle harder to implement. This code is a modified version of yours which loads the information, sets the title to the Color property, and sets the subtitle to a string containing the Rvalue, Gvalue, and Bvalue properties.

    - (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;
    }
    
    0 讨论(0)
提交回复
热议问题