簡體   English   中英

UItableViewCell DequeueReusableCellWithIdentifier removeFromSuperview

[英]UItableviewcell dequeueReusableCellWithIdentifier removeFromSuperview

這可能是一個簡單的問題,但是我還沒有找到正確的解決方案,所以我希望有人可以提供幫助。

我從情節提要中獲得了一個帶有三個標簽的單元格的表格視圖。 所有標簽都通過IBOutlet連接,並具有正確的約束。

問題是我想基於數據刪除第二個標簽,以便保留約束,而第三個標簽使用其較低的約束向第一個標簽靠攏。

但是當我運行以下代碼時:

- (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;
}

該代碼可以完美工作,直到我向下滾動並重新使用單元格為止。 然后,第二個標簽為“已消失”(removeFromSuperview),因此不可見。

有沒有一種快速的方法,而無需在代碼中創建標簽(帶有約束)並添加/刪除它呢?

希望這一切都有道理,否則請詢問。

UPDATE

解決方案是隱藏標簽並在約束上設置優先級。

創建要切換的約束的IBOutlet

使用以下代碼進行編輯。

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;

    }

不要從單元格中刪除任何東西,只要能見度就可以處理

      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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM