【问题标题】:Putting Tableview inside UITableViewCell Swift将 Tableview 放入 UITableViewCell Swift
【发布时间】:2017-01-01 20:56:47
【问题描述】:

我在将 tableview 放入 uitableview 单元格时遇到问题。现在,它甚至不显示嵌套的 tableview。我将 tableview 的委托和数据源设置为自定义单元格,并拥有所有必需的方法。我还在为我的表格视图使用故事板,并正确连接了所有插座。

问题在于它没有进入 cellForRow 函数。但是,它确实进入了 numberOfRowsInSection 函数以及 heightForRow 函数。下面是包含嵌套 UITableviewcell 的自定义单元格的代码。

import UIKit

class EventSurveyCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var cellBox: UIView!
@IBOutlet weak var announcementLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var authorLabel: UILabel!
@IBOutlet weak var numVotesLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
var surveyArray: [SurveyClass] = []

override func awakeFromNib() {
    super.awakeFromNib()
    tableView.delegate = self
    tableView.dataSource = self
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    tableView.delegate = self
    tableView.dataSource = self

    // Configure the view for the selected state
}

func do_table_refresh()
{
    dispatch_async(dispatch_get_main_queue(), {
        self.tableView.reloadData()
        return
    })
}

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

    cell.itemLabel.text = surveyArray[indexPath.row].itemName

    //go through firebase to check which one user voted on
    cell.voteButton.imageView!.image = UIImage(named: "circle")

    let percent = surveyArray[indexPath.row].numOfVotes/surveyArray[indexPath.row].totalNumOfVotes
    cell.precentLabel.text = String(percent)

    return cell

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    print("heightforrow")
    return 44
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("numrowsinsection")
    return 3
    //should be return self.surveyArray.count. 

}
}

下面是保存自定义单元格(包含嵌套表格视图)的视图控制器中的代码的 sn-p。此代码是 cellforrowatindexpath 的一部分

   case PostType.survey:

        let cell: EventSurveyCell = self.tableView.dequeueReusableCellWithIdentifier("EventSurveyCell") as! EventSurveyCell!
        //cut out other code as it is irrelevant
        cell.surveyArray = eventPost.surveyClassArray

        cell.do_table_refresh()
        print(cell.tableView.rowHeight)

        return cell

我注意到的另一个问题是,当打印 cell.tableview.rowheight 时,它会打印 -1。这可能是它没有进入 cellforrowatindexpath 的原因,但我在 heightforrow 函数中明确返回 44。现在,它甚至不显示嵌套的 tableview。

【问题讨论】:

    标签: swift uitableview nested


    【解决方案1】:

    1) 不需要do_table_refresh,可以直接拨打cell.tableView.reloadData()

    2) 重写systemLayoutSizeFitting:withHorizo​​ntalFittingPriority:verticalFittingPriority函数:

    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
    
        tableView.reloadData()
    
        // if the table view is the last UI element, you might need to adjust the height
        let size = CGSize(width: targetSize.width,
                          height: tableView.frame.origin.y + tableView.contentSize.height)
        return size
    
    }
    

    3) 在您的视图控制器中,设置surveyArray 后,删除此行cell.do_table_refresh(),替换为cell.setNeedsLayout() 以自动调整布局(您可能还需要调用一个方法来更新其他UI,例如作为announcementLabel 等在setNeedsLayout 之前调用它。)

    【讨论】:

    • 我试过这个,这适用于嵌套 tableview 单元格内的单行标签,但是如果我们在嵌套 tableview 单元格中有多行标签怎么办?
    • 如果你有多个标签,你可以使用 UIStackView 来托管所有的标签。
    • 使用的是 iOS 8,没有可用的 stackview
    • 字面上花了很长时间试图解决这个问题,这很有效!你真的是最棒的!非常感谢
    【解决方案2】:

    我遇到了同样的问题,cellForRow 没有被调用自定义单元格内的 TableView。原因是 tableview 没有设置 height 来显示其中的内容。在我为子 TableView 设置了一些高度后,cellForRow 开始调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多