【问题标题】:Dynamic Cell Height Size Doesn't Work动态单元格高度大小不起作用
【发布时间】:2015-10-19 03:05:23
【问题描述】:

我正在使用 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

【问题讨论】:

  • 不幸的是,您需要使用自定义单元原型。见stackoverflow.com/questions/28931295/…
  • @jrc 不是真的,你能告诉我你的笔尖吗?自动布局?可能值得在演示项目中制作它以供其他人使用。
  • 容易重现问题。只需使用主从应用程序模板。将表格视图rowHeightestimatedRowHeight 设置为stackoverflow.com/questions/18746929/…,将情节提要中的单元格样式更改为字幕,numberOfLinestextLabeldetailTextLabel 设置为0,并在-tableView:cellForRowAtIndexPath: 中设置文本两者都长。动态高度适用于textLabel,但不适用于detailTextLabel

标签: ios iphone swift cocoa-touch ios8


【解决方案1】:

除了创建@jrc 提到的自定义单元原型之外,我找不到任何解决方案。所以解决方案是创建自定义单元原型并使用自动布局。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多