[英]UItableviewcell dequeueReusableCellWithIdentifier removeFromSuperview
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. 所有标签都通过
IBOutlet
连接,并具有正确的约束。
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.
然后,第二个标签为“已消失”(removeFromSuperview),因此不可见。
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 UPDATE
The solution is to hide to label and set the priorities on the constraints. 解决方案是隐藏标签并在约束上设置优先级。
Create IBOutlet of the constraints you want to switch 创建要切换的约束的IBOutlet
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;
}
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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.