2015-03-03 103 views
2

我無法讓UILabel自動換行而不設置明確的首選寬度。請幫忙。UITableViewCell計算正確的高度,但UILabel不包裝

這裏是我的界面生成器的UILabel在一個UITableViewCell

UILabel IB Attributes Inspector UILabel IB Size Inspector

我的結果是:

Application Example Result

但是,如果我指定一個明確的爲preferredWidth爲:

explicit height

我得到如下:

With wordwrapping

就算它有一些怪異的垂直間距,但至少它的包裝正確。

而且,這裏就是我要建行中我的UITableViewController:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! { 

    let cell = tableView.dequeueReusableCellWithIdentifier(Constants.CellReuseIdentifier, forIndexPath: indexPath) as? BurnFeedTableViewCell ?? 
     BurnFeedTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: Constants.CellReuseIdentifier) 

    cell.nameOfBurnLabel?.text = object.objectForKey("name") as? String 
    if let owner = object.objectForKey("owner") as? PFUser { 
     cell.ownerLabel?.text = owner.username 
    } else { 
     cell.ownerLabel?.text = "unknown" 
    } 

    return cell 
} 

和定義的rowHeight & estimatedRowHeigh爲:

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.estimatedRowHeight = 100.0 
    self.tableView.rowHeight = UITableViewAutomaticDimension 
} 

我真的很想得到這個工作,而不必指定明確的高度。另外,我玩弄了contentCompressionResistancePriority,但沒有運氣。

這肯定是類似於下面的SO問題:Using iOS 8 flexible table cells, cell height changes but text doesn't wrap

然而,該解決方案似乎有沒有工作。事實上,也有一些類似的問題。儘管我似乎無法讓他們工作。

編輯:從main.storyboard

<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Burn Feed Cell" 
       rowHeight="95" id="bif-hV-Vqt" customClass="BurnFeedTableViewCell" customModule="Burn" 
       customModuleProvider="target"> 
    <rect key="frame" x="0.0" y="0.0" width="600" height="44"/> 
    <autoresizingMask key="autoresizingMask"/> 
    <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" 
           contentMode="center" tableViewCell="bif-hV-Vqt" id="KGP-Sx-cZC"> 
     <autoresizingMask key="autoresizingMask"/> 
     <subviews> 
      <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" 
        verticalHuggingPriority="251" text="owner" lineBreakMode="tailTruncation" 
        baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" 
        translatesAutoresizingMaskIntoConstraints="NO" id="TZ9-cx-K29"> 
       <rect key="frame" x="8" y="37" width="48" height="21"/> 
       <fontDescription key="fontDescription" type="system" pointSize="17"/> 
       <color key="textColor" cocoaTouchSystemColor="darkTextColor"/> 
       <nil key="highlightedColor"/> 
      </label> 
      <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" 
        verticalHuggingPriority="251" text="Burn Name" lineBreakMode="wordWrap" numberOfLines="0" 
        baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="90" 
        translatesAutoresizingMaskIntoConstraints="NO" id="v96-dx-9z5"> 
       <rect key="frame" x="8" y="8" width="584" height="21"/> 
       <fontDescription key="fontDescription" type="system" pointSize="17"/> 
       <color key="textColor" cocoaTouchSystemColor="darkTextColor"/> 
       <nil key="highlightedColor"/> 
      </label> 
      <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" 
        verticalHuggingPriority="251" text="countdown" lineBreakMode="tailTruncation" 
        baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" 
        translatesAutoresizingMaskIntoConstraints="NO" id="6YZ-ne-79f"> 
       <rect key="frame" x="506" y="37" width="86" height="21"/> 
       <fontDescription key="fontDescription" type="system" pointSize="17"/> 
       <color key="textColor" cocoaTouchSystemColor="darkTextColor"/> 
       <nil key="highlightedColor"/> 
      </label> 
     </subviews> 
     <constraints> 
      <constraint firstItem="v96-dx-9z5" firstAttribute="leading" secondItem="KGP-Sx-cZC" 
         secondAttribute="leadingMargin" id="Iwe-kT-1Do"/> 
      <constraint firstItem="6YZ-ne-79f" firstAttribute="trailing" secondItem="KGP-Sx-cZC" 
         secondAttribute="trailingMargin" id="JOA-d6-q3X"/> 
      <constraint firstItem="6YZ-ne-79f" firstAttribute="baseline" secondItem="TZ9-cx-K29" 
         secondAttribute="baseline" id="JXU-Hf-vVb"/> 
      <constraint firstItem="TZ9-cx-K29" firstAttribute="leading" secondItem="KGP-Sx-cZC" 
         secondAttribute="leadingMargin" id="VOG-Xh-9wd"/> 
      <constraint firstAttribute="bottomMargin" relation="greaterThanOrEqual" secondItem="TZ9-cx-K29" 
         secondAttribute="bottom" constant="5" id="am1-l0-8EV"/> 
      <constraint firstItem="TZ9-cx-K29" firstAttribute="top" secondItem="v96-dx-9z5" secondAttribute="bottom" 
         constant="8" symbolic="YES" id="jLQ-yQ-A8V"/> 
      <constraint firstItem="v96-dx-9z5" firstAttribute="trailing" secondItem="KGP-Sx-cZC" 
         secondAttribute="trailingMargin" id="sl2-Fb-sWy"/> 
      <constraint firstItem="v96-dx-9z5" firstAttribute="top" secondItem="KGP-Sx-cZC" secondAttribute="topMargin" 
         id="xt3-Em-EaP"/> 
     </constraints> 
    </tableViewCellContentView> 
    <connections> 
     <outlet property="countdownLabel" destination="6YZ-ne-79f" id="te1-QF-Nuo"/> 
     <outlet property="nameOfBurnLabel" destination="v96-dx-9z5" id="Wki-e1-0Np"/> 
     <outlet property="ownerLabel" destination="TZ9-cx-K29" id="wqh-Cj-yQX"/> 
    </connections> 
</tableViewCell> 

回答

0

您的問題tableViewCell的XML是自動佈局。爲了獲得正確的寬度/高度包裝,您需要確保您的UITableViewCell中的所有元素都從單元格的左邊緣向右和從頂部邊緣到底部進行了適當約束。

+0

我意識到這一點,一切都應該能夠計算出它的x和你沒有歧義的座標。我已經玩過各種各樣的尺寸而沒有運氣。如果我要分享我的限制,你能告訴我需要解決什麼問題嗎? – 2015-03-03 02:28:24

+0

當然。發佈所有關於單元格元素的約束與你的問題 – kellanburket 2015-03-03 02:30:30

+0

編輯問題與主題tableview的xml從main.storyboard – 2015-03-03 03:37:46

0

您是否禁用了所有標籤的自動調整掩碼轉換?

cell.nameOfBurnLabel.setTranslatesAutoresizingMaskIntoConstraints(false) 
cell.ownerLabel.setTranslatesAutoresizingMaskIntoConstraints(false) 
cell.countdownLabel.setTranslatesAutoresizingMaskIntoConstraints(false) 

至少,這是我必須做的,以獲得動態細胞高度使用標籤。

+1

因爲Cell是故事板的一部分,所以不需要 – 2015-03-03 07:16:27

相關問題