0

I am using swift 2.0 and trying to create table view without custom cell prototype. I wrote this:

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
  }

I am expecting to enlarge cells by detailTextLabel size but they remains same and they are overlaps: http://i61.tinypic.com/1shb3b.jpg

mTuran
  • 1,846
  • 4
  • 32
  • 58
  • 1
    Unfortunately, you need to use a custom cell prototype. See http://stackoverflow.com/questions/28931295/self-sizing-dynamic-height-cells-in-ios-8-possible-without-custom-uitablevie – jrc Oct 19 '15 at 03:10
  • @jrc not true, can you show me your nib? with auto layout? It might be worth producing this in a demo project for others to play with. – Oliver Atkinson Oct 22 '15 at 07:56
  • Easy to repro the problem. Just use the Master-Detail Application template. Set the table view `rowHeight` and `estimatedRowHeight` as in http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights, change the cell style in storyboard to Subtitle and `numberOfLines` for both `textLabel` and `detailTextLabel` to 0, and in `-tableView:cellForRowAtIndexPath:`, set the text for both to something long. Dynamic height works for `textLabel` but not `detailTextLabel`. – jrc Oct 22 '15 at 11:42

1 Answers1

0

I can't found any solution except creating custom cell prototype as @jrc mentioned. So solution is creating custom cell prototypes and using auto layout.

mTuran
  • 1,846
  • 4
  • 32
  • 58