2015-03-03 89 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

相关问题