2017-01-01 76 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之前調用它。)