2015-10-19 65 views
0

我正在使用swift 2.0并尝试创建没有自定义单元格原型的表格视图。我写了这个:动态单元格高度大小不起作用

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.backgroundColor = UIColor(patternImage: UIImage(named: "ViewBg.png")!) 

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

    Alamofire.request(.GET, "https://****.herokuapp.com/user/000", parameters: nil) 
     .responseJSON { response in 
     let fortuneHistoryJson = JSON(response.result.value!)["fortuneHistory"] 

     for var index = 0; index < fortuneHistoryJson.count; ++index { 
      let FortuneHistoryItem = FortuneHistory() 
      FortuneHistoryItem.content = fortuneHistoryJson[index]["fortune"]["content"].string 
      FortuneHistoryItem.date = fortuneHistoryJson[index]["createdAt"].string 
      FortuneHistoryItem.imageUrl = fortuneHistoryJson[index]["cupPicture"].string 
      self.fortuneHistoryData.append(FortuneHistoryItem) 
     } 
     self.tableView.reloadData(); 
    } 
    } 

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

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

    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell") 
    cell.textLabel!.text = self.fortuneHistoryData[indexPath.row].date; 
    cell.detailTextLabel!.text = self.fortuneHistoryData[indexPath.row].content 
    cell.textLabel!.numberOfLines = 0 
    cell.detailTextLabel!.numberOfLines = 0 

    cell.imageView!.kf_setImageWithURL(NSURL(string: self.fortuneHistoryData[indexPath.row].imageUrl)!, placeholderImage: UIImage(named: "ViewBg.png")!) 
    var itemSize: CGSize = CGSizeMake(100, 100) 
    UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale) 
    var imageRect: CGRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height) 
    cell.imageView!.image!.drawInRect(imageRect) 
    cell.imageView!.image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 


    return cell 
    } 

我期待通过detailTextLabel尺寸放大细胞,但它们仍然是相同的,他们是重叠:http://i61.tinypic.com/1shb3b.jpg

+1

不幸的是,您需要使用自定义单元格原型。见http://stackoverflow.com/questions/28931295/self-sizing-dynamic-height-cells-in-ios-8-possible-without-custom-uitablevie – jrc

+0

@jrc不是真的,你能告诉我你的笔尖吗?与自动布局? 可能值得在演示项目中为其他人玩这个游戏。 –

+0

易于重现问题。只需使用Master-Detail应用程序模板即可。设置表格视图'rowHeight'和'estimatedRowHeight',如http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights,改变故事板中的单元格样式为Subtitle,'textLabel'和'detailTextLabel'为'numberOfLines'为0,在'-tableView:cellForRowAtIndexPath:'中,将文本设置为long。动态高度适用于'textLabel',但不适用于'detailTextLabel'。 – jrc

回答

0

我无法找到任何解决办法,除了提到的创建自定义的电池原型@jrc 。所以解决方案是创建自定义单元格原型并使用自动布局。