将uitableview的行高调整为他的内容

时间:2015-10-29 08:30:57

标签: ios swift uitableview height row

您好我试图将我的桌面视图的行高调整到他的内容,但高度不是很好:

enter image description here

对于单元格,我使用UITableViewCell的Cocoa Touch类子类:

enter image description here

我尝试了这两行:

    tableView.estimatedRowHeight = 160.0

    tableView.rowHeight = UITableViewAutomaticDimension

但它没有用。

override func viewDidLoad()
{
    super.viewDidLoad()
    self.navigationController?.navigationBar.barTintColor = UIColor(red: 38.0/255.0, green: 51.0/255.0, blue: 85.0/255.0, alpha: 1.0)
    self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Gotham", size: 13)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
    self.title = "ACTUALITÉS"

    let nib = UINib(nibName: "actuVwCell", bundle: nil)
    tableView.registerNib(nib, forCellReuseIdentifier: "cell")


    self.beginParsing()

    tableView.estimatedRowHeight = 160.0

    tableView.rowHeight = UITableViewAutomaticDimension
}

修改

enter image description here

ActualitesViewController:

import UIKit

@objc
protocol ActualitesViewControllerDelegate {
    optional func toggleLeftPanel()
    optional func collapseSidePanels()
}

class ActualitesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSXMLParserDelegate {
    @IBOutlet var tableView: UITableView!

    var parser = NSXMLParser()
    var posts = NSMutableArray()
    var elements = NSMutableDictionary()
    var element = NSString()
    var title1 = NSMutableString()
    var date = NSMutableString()
    var dscrptn = NSMutableString()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 38.0/255.0, green: 51.0/255.0, blue: 85.0/255.0, alpha: 1.0)
        self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Gotham", size: 13)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
        self.title = "ACTUALITÉS"

        let nib = UINib(nibName: "actuVwCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "cell")


        self.beginParsing()

        tableView.estimatedRowHeight = 160.0
    }

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

    func beginParsing()
    {
        posts = []
        parser = NSXMLParser(contentsOfURL:(NSURL(string:"http:...rss.html"))!)!
        parser.delegate = self
        parser.parse()

        tableView!.reloadData()
    }

    //XMLParser Methods

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
    {
        element = elementName
        if (elementName as NSString).isEqualToString("item")
        {
            elements = NSMutableDictionary()
            elements = [:]
            title1 = NSMutableString()
            title1 = ""
            date = NSMutableString()
            date = ""
            dscrptn = NSMutableString()
            dscrptn = ""
        }
    }

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
    {
        if (elementName as NSString).isEqualToString("item") {
            if !title1.isEqual(nil) {
                elements.setObject(title1, forKey: "title")
            }
            if !date.isEqual(nil) {
                elements.setObject(date, forKey: "date")
            }
            if !dscrptn.isEqual(nil) {
                elements.setObject(dscrptn, forKey: "dscrptn")
            }

            posts.addObject(elements)
        }
    }

    func parser(parser: NSXMLParser, foundCharacters string: String)
    {
        if element.isEqualToString("title") {
            title1.appendString(string)
        } else if element.isEqualToString("pubDate") {
            date.appendString(string)
        } else if element.isEqualToString("description") {
            dscrptn.appendString(string)
        }
    }

    //Tableview Methods

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return posts.count
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell:ActuTblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! ActuTblCell

        /*
        if(cell.isEqual(NSNull)) {
            cell = NSBundle.mainBundle().loadNibNamed("cell", owner: self, options: nil)[0] as! ActuTblCell;
        }
        */

        cell.titleActuCell.numberOfLines = 0
        cell.titleActuCell.lineBreakMode = NSLineBreakMode.ByWordWrapping
        cell.titleActuCell.sizeToFit()

        cell.dateActuCell.numberOfLines = 0
        cell.dateActuCell.lineBreakMode = NSLineBreakMode.ByWordWrapping
        cell.dateActuCell.sizeToFit()

        cell.descriptionActuCell.numberOfLines = 0
        cell.descriptionActuCell.lineBreakMode = NSLineBreakMode.ByWordWrapping
        cell.descriptionActuCell.sizeToFit()


        cell.titleActuCell.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as! NSString as String

        cell.dateActuCell.text = posts.objectAtIndex(indexPath.row).valueForKey("date") as! NSString as String


        cell.descriptionActuCell.text = posts.objectAtIndex(indexPath.row).valueForKey("dscrptn") as! NSString as String

        return cell
    }

}

2 个答案:

答案 0 :(得分:2)

您应该使用AutoLayout,并且需要确保添加了所有垂直约束(从上到下)。 我尝试使用时遇到了同样的问题:

tableView.rowHeight = UITableViewAutomaticDimension

解决方案是移除该行并将其放入heightForRowAtIndexPath方法:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

答案 1 :(得分:0)

尝试

override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
  return 160
}