将Tableview放在UITableViewCell Swift中

Pat*_*ick 1 nested uitableview swift

我在将一个tableview放在一个uitableview单元格中时遇到了麻烦.现在,它甚至没有显示嵌套的tableview.我将tableview的委托和数据源设置为自定义单元格,并具有所有必需的方法.我也在为我的tableviews使用故事板,并正确连接所有插座.

问题是它没有进入cellforrow功能.但是,它确实进入了numberofrowinsection函数,以及heightforrow函数.下面是包含嵌套的uitableviewcell的自定义单元格的代码.

导入UIKit

class EventSurveyCell:UITableViewCell,UITableViewDelegate,UITableViewDataSource {

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. 

}
}
Run Code Online (Sandbox Code Playgroud)

}

下面是一个代码片段,用于在viewcontroller中保存自定义单元格(包含嵌套的tableviews).此代码是cellforrowatindexpath的一部分

case PostType.survey:

   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
Run Code Online (Sandbox Code Playgroud)

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

bub*_*uxu 7

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

}
Run Code Online (Sandbox Code Playgroud)

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