2017-01-01 81 views
1

我在将一个tableview放入一个uitableview单元格时遇到了麻烦。现在,它甚至不会显示嵌套的tableview。我将tableview的委托和数据源设置为自定义单元格,并具有所有必需的方法。我还为我的桌面视图使用了故事板,并已正确连接所有插座。把Tableview放入UITableViewCell Swift

问题是它没有进入cellforrow函数。但是,它会进入numberofrowsinsection函数以及heightforrow函数。下面是包含嵌套的uitableviewcell的自定义单元的代码。

进口的UIKit

类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. 

} 

}

下面是代码在所述的ViewController持有该定制单元的一个片段(含有嵌套tableviews)。此代码是的cellForRowAtIndexPath

情况下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的原因,但我明确地返回44在heightforrow功能。现在,它甚至不显示嵌套的tableview。

回答

3

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之前调用它。)