简体   繁体   English

UItableViewCell DequeueReusableCellWithIdentifier removeFromSuperview

[英]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.

相关问题 UITableViewCell&#39;没有成员&#39;dequeueReusableCellWithIdentifier&#39; - UITableViewCell' has no member 'dequeueReusableCellWithIdentifier' 自定义UITableViewCell没有dequeueReusableCellWithIdentifier - Custom UITableViewCell without dequeueReusableCellWithIdentifier 具有自定义初始化程序的UITableViewCell dequeueReusableCellWithIdentifier - UITableViewCell dequeueReusableCellWithIdentifier with custom initializer UITableViewCell样式和dequeueReusableCellWithIdentifier - UITableViewCell style and dequeueReusableCellWithIdentifier UITableViewCell dequeueReusableCellWithIdentifier:forIndexPath:问题 - UITableViewCell dequeueReusableCellWithIdentifier:forIndexPath: issue UITableViewCell-prepareForReuse和dequeueReusableCellWithIdentifier - UITableViewCell - prepareForReuse and dequeueReusableCellWithIdentifier segue不使用UITableViewCell alloc,但是dequeueReusableCellWithIdentifier - segue not working with UITableViewCell alloc, but dequeueReusableCellWithIdentifier UITableViewCell dequeuereusablecellwithidentifier返回相同的单元格 - UITableViewCell dequeuereusablecellwithidentifier returns the same cell 子类化UITableViewCell和[tableView dequeueReusableCellWithIdentifier:CellIdentifier] - Subclassing UITableViewCell & [tableView dequeueReusableCellWithIdentifier:CellIdentifier] UITableViewCell的子视图似乎在dequeueReusableCellWithIdentifier之后随机消失 - Subviews of UITableViewCell seem to randomly disappear after dequeueReusableCellWithIdentifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM