2016-04-25 10 views
6

iが字幕・ラベルがnumberoflines = 0デフォルトのUITableViewのセル自動高さ

を持っていると私は

tableView.rowHeight = UITableViewAutomaticDimension 
tableView.estimatedRowHeight = 44.0 

をすれば、細胞が自動的に行ういけないスタイルuitableviewcellstylesubtitleのセルとのUITableViewControllerを、持っています高さ、それは本当に真実である - あなたはデフォルトのスタイルのセルでそれを使用することはできません?

EDIT:

シンプル人口

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
    let thisHelpArticle = helpObjects[indexPath.section] 

    cell.textLabel!.text = thisHelpArticle.helpHeadline 
    cell.detailTextLabel!.text = thisHelpArticle.helpDescription 

    return cell 
} 
+0

詳細情報が必要です。細胞をどのように移植するかを教えてください。さらに、オートレイアウトを使用していますか? – ozgur

+0

前述したように、Subtitleスタイルのデフォルトセルを持つuitableviewcontrollerは、autolayoutを追加できないので、人口を上に追加します – magnuskahr

+0

セルを返す前に 'cell.layoutIfNeeded()'を呼び出すことはできますか? – ozgur

答えて

4

UPDATE! デフォルトのセルサブタイトルがセルを自動サイズ調整するには、ラベルの行数を0に設定する必要があります。それ以外の場合は動作しません。奇妙な。

cell.textLabel?.numberOfLines = 0 
cell.detailTextLabel?.numberOfLines = 0 

デフォルトのセルを自動サイズ変更することができます。最も単純な形式で私は次のコードを持っています。既定のスタイル付き字幕セルのサイズを自動的に変更します。

class ViewController: UIViewController { 

    @IBOutlet var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.dataSource = self 
     tableView.delegate = self 
     tableView.rowHeight = UITableViewAutomaticDimension 
     tableView.estimatedRowHeight = 44 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

extension ViewController: UITableViewDataSource { 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 2 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
     let thisHelpArticle = "Lorem ipsum dolor sit amet, vel porttitor blandit, aliquam tristique vestibulum, enim vel eros fames id, in gravida vestibulum gravida tempor, et vel libero sed mauris. Suspendisse ut placerat viverra dictum, ante ante vel ut vestibulum sollicitudin phasellus. Dictumst adipiscing adipiscing nisl, fusce ut. Ante wisi pellentesque, et aliquam rhoncus eget convallis quam voluptate, ut nec quis, sodales ullamcorper elementum pellentesque sagittis vitae, dolor justo fermentum amet risus. Eu placerat ultricies. Ipsum sodales, massa elit, in neque, sed penatibus gravida, cursus eget. Ut tincidunt at eu, wisi dis vel penatibus eget, volutpat ligula vel tortor morbi feugiat, dui et eiusmod dis sociis. Iaculis lorem molestie laoreet sit, orci commodo, fusce vestibulum sapien, quisque egestas maecenas sed rem in nisl." 

     cell.textLabel?.numberOfLines = 0 
     cell.detailTextLabel?.numberOfLines = 0 

     cell.textLabel!.text = thisHelpArticle 
     cell.detailTextLabel!.text = thisHelpArticle 

     return cell 
    } 
} 

extension ViewController: UITableViewDelegate { 

} 
+0

私はあなたがそれを書いたのと同じように考え出しました、細胞は大きくなりますが、私が見ることができるようにいくつかのテキストを切り捨てます。 – magnuskahr

+0

確かにそうです。私はそれをサブクラス化する必要があると思います。奇妙だけど。質問を喚起するためにUpvote。 – oyalhi

関連する問題