Resolved User editable .plist for UITableView

Discussion in 'iOS Programming' started by troop231, Nov 20, 2012.

  1. troop231, Nov 20, 2012
    Last edited: Nov 22, 2012

    macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #1
    My app uses a .plist as it's table data source, but I want to add the ability for my users to hide and unhide certain cells that don't interest them.

    I know that I can put the table into editing mode and have the user swipe to delete cells, but that isn't what I'm looking for because the user may want to restore those deleted cells at a later time, or if they mistakenly deleted a cell.

    I'm looking for the checkmark type of cell marking/unmarking so the user can create a "favorites" list if you will. When the user is done being in the table "edit" mode, he/she will tap done, and the app will save what cells are to be hidden from being displayed in the table view into a settings .plist.

    How could I go about doing this?

    Thank you for any help offered.
     
  2. macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #2
    Pretty straight forward. Just keep a boolean for each of your table cell model objects which tells if it is to be shown or not.
     
  3. troop231, Nov 21, 2012
    Last edited: Nov 21, 2012

    thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #3
    By using the .hidden property for each cell?

    I tried this, but my table view doesn't compensate for the empty space left by the hidden cells.

    Is there a way to compensate by using
    Code:
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    Screenshot: [​IMG]

    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        
        NSArray *documentsPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *PlistPath = [NSString stringWithFormat:@"%@/%@", [documentsPath objectAtIndex:0],@"Table.plist"];
        
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:PlistPath];
        
        NSInteger row = [indexPath row];
        
        // Set up the cell...
    	NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
        
        if(row == 4 && ([[dict objectForKey:@"4"]isEqualToString:@"0"])){
            cell.hidden = YES;
        }
        if(row == 7 && ([[dict objectForKey:@"7"]isEqualToString:@"0"])){
            cell.hidden = YES;
        }
        
        else {
        
        tableView.separatorColor = [UIColor clearColor];
    	cell.textLabel.text = [dictionary objectForKey:@"Title"];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor colorWithRed:0.071 green:0.204 blue:0.565 alpha:1];
    }
    return cell;
    
    }
    
     
  4. macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #4
    You are not hiding the cell. You are hiding the contents of the cell.
    What you need to do is keep only objects that are to be shown in your table's data source array.

    You can do this by reading the array from the plist and filtering it so it contains only those cells which are to be shown.
     
  5. troop231, Nov 21, 2012
    Last edited: Nov 21, 2012

    thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #5
    So if I understand correctly, I need to parse my app's table data before displaying it based upon the user's settings in the .plist? Do you know how I could do this? I'm having a hard time trying to grasp it, because my app gets it's data like this:

    Code:
    NSArray *tempArray = [[NSArray alloc] init];
    		self.tableDataSource = tempArray;
    		
    		AppDelegate *AppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    		self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
    
    Should I use NSPredicate to search the user's settings plist and then use that to display the filtered user's table data?
     
  6. macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #6
    Exactly. You can use NSPredicate to filter the array after fetching it. Like this:

    Code:
    AppDelegate *AppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    // First fetch all objects
    NSMutableArray *allObjects = [NSMutableArray arrayWithArray:[AppDelegate.data objectForKey:@"Rows"]];
    
    // Create a predicate
    // shouldBeHidden is the name of the boolean property
    NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"shouldBeHidden == 0"];
    
    // Filter the array
    [allObjects filterUsingPredicate:aPredicate];
    
    // Set it as the data source
    self.tableDataSource = allObjects;
    
    
     
  7. thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #7
    Thank you for that!

    How would I set the predicate to search for the boolean keys in a different .plist? The user's settings are stored in a different plist than the table's source.
     
  8. macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #8
    I didn't quite get you. But you can filter any array using the same logic. Load the array from the plist, create a mutable array with it, create the predicate and filter.
     
  9. thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #9
    Sorry, I probably didn't explain it right.

    I have solved it now though however by using the code you created. I can't believe I overlooked it.

    Thank you again sir!
     
  10. macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #10
    Happy to help :)
     

Share This Page