Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

troop231

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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.
 
Last edited:

CodeBreaker

macrumors 6502
Nov 5, 2010
494
1
Sea of Tranquility
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.

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.
 

troop231

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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.

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:
impn6.png


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;

}
 
Last edited:

CodeBreaker

macrumors 6502
Nov 5, 2010
494
1
Sea of Tranquility
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: Image

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;

}

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.
 

troop231

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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.

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?
 
Last edited:

CodeBreaker

macrumors 6502
Nov 5, 2010
494
1
Sea of Tranquility
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?

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;
 

troop231

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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;

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.
 

CodeBreaker

macrumors 6502
Nov 5, 2010
494
1
Sea of Tranquility
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.

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.
 

troop231

macrumors 603
Original poster
Jan 20, 2010
5,822
553
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.

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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.