Reputation: 45
This is probably a simple problem, but I haven't found the right solution yet, so I am hoping somebody can help.
I have a tableview from storyboard with a cell with three labels.
All labels are connected via IBOutlet
and have correct constrains.
The issue is that I want to remove the second label based on data so the constraints are kept and the third label uses its lower constraints to move closer to the first label.
But when I run the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"GroupMaterialCell";
GroupMaterialCell *cell = (GroupMaterialCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
// get data and populate row
if([self.materialitems count] >= indexPath.row) {
Material *material = [self.materialitems objectAtIndex:indexPath.row];
cell.folder.text = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"Folder", nil), material.folder];cell.name.text = material.name;;
cell.member.text = material.member_name;
cell.date.text = [material getDate];
cell.info.text = [NSString stringWithFormat:@"%@, %@", [material getType], [material getFilesize]];
if([material.folder length] <= 0) {
[cell.folder removeFromSuperview];
}
}
return cell;
}
The code works perfect until I scroll down and it reuses the cells. Then the second label is "gone" (removeFromSuperview) and therefore not visible.
Is there a quick way to do this without creating the label in code (with constrainst) and add / remove it?
Hope it all makes sense, otherwise ask.
UPDATE
The solution is to hide to label and set the priorities on the constraints.
Create IBOutlet of the constraints you want to switch
Edit code with following.
if([material.folder length] <= 0) {
[cell.folder setHidden:YES];
cell.memberToFolder.priority = 500;
cell.memberToName.priority = 900;
} else {
[cell.folder setHidden:NO];
cell.memberToFolder.priority = 900;
cell.memberToName.priority = 500;
}
Upvotes: 0
Views: 736
Reputation: 2294
Do not remove any thing from cell just handle it with it's visibility
if([material.folder length] <= 0) {
//set frame if required
[cell.folder setHidden:true];
} else {
//set frame if required
[cell.folder setHidden:false];
}
Upvotes: 1