Reputation: 1134
I am getting data from a MySQL database with AFNetworking, the data is coming in, and in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I have the code to check whether the current logged in user is equal to the field 'creator' in MySQL, the entries show but I want the empty cells to be hidden.
if ([creator isEqual: userID]) {
[cell.contentView addSubview:imageView];
cell.textLabel.text = [[NSString alloc] initWithFormat:@" %@",[tempDictionary objectForKey:@"team_name"]];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@ %@ \n%@", [tempDictionary objectForKey:@"date"], [tempDictionary objectForKey:@"time"], [tempDictionary objectForKey:@"location"]];
} else {
[imageView removeFromSuperview];
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
}
The UITableView is just showing the data for the selected fields but also showing all the other empty cells (as it is still counting the array).
Upvotes: 1
Views: 3041
Reputation: 13546
Cells can be hidden by setting height to zero in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
.
First option is to set some flag for hiding empty cells.
In (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if ([creator isEqual: userID]) {
[cell.contentView addSubview:imageView];
cell.textLabel.text = [[NSString alloc] initWithFormat:@" %@",[tempDictionary objectForKey:@"team_name"]];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@ %@ \n%@", [tempDictionary objectForKey:@"date"], [tempDictionary objectForKey:@"time"], [tempDictionary objectForKey:@"location"]];
hideCells = NO;
} else {
[imageView removeFromSuperview];
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
hideCells = YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == someCell && hideCells == YES)
return 0;
else
return 44;
}
Second option is that you add extra key-value pair in your data source dictionary to keep track if you need to hide a cell or not.
if ([creator isEqual: userID]) {
[cell.contentView addSubview:imageView];
cell.textLabel.text = [[NSString alloc] initWithFormat:@" %@",[tempDictionary objectForKey:@"team_name"]];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@ %@ \n%@", [tempDictionary objectForKey:@"date"], [tempDictionary objectForKey:@"time"], [tempDictionary objectForKey:@"location"]];
[array replaceObjectAtIndex:indexPath.row withObject:@"YES"];
} else {
[imageView removeFromSuperview];
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
[array replaceObjectAtIndex:indexPath.row withObject:@"YES"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[array objectAtIndex:indexPath.row]isEqualToString:@"NO"] )
return 0;
else
return 44;
}
Upvotes: 0
Reputation: 364
The only way for doing this is to update your datasource array, you need to remove that object from the data source array as well. And if you want to maintain that values as well then you can maintain one more array and you can filter that array to get all objects which have values and filled datasource array with filtered objects using predicate. I hope this will help you.
Upvotes: 2
Reputation: 10172
I suggest you to prepare anotherArray
from your masterArray
with your condition (i.e. ([creator isEqual: userID])
) before loading tableView
and load tableView
using this anotherArray
.
Upvotes: 1