2017-01-01 19 views
1

uitableviewセル内にテーブルビューを配置することに問題があります。今は、ネストしたテーブルビューを表示しません。テーブルビューのデリゲートとデータソースをカスタムセルに設定し、必要なすべてのメソッドを用意しました。私はまた、私のテーブルビューのためにストーリーボードを使用しており、すべてのアウトレットを適切に接続しています。TableviewをUITableViewCell Swift内に配置

問題は、セルフォロー機能には入っていないということです。しかし、それは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(含有でのコードの抜粋でありますネストされたテーブルビュー)。このコードは、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に入らない理由かもしれませんが、私は明らかにheightforrow関数で44を返します。今は、ネストされたテーブルビューを表示しません。

答えて

3

1)do_table_refreshが不要な場合は、cell.tableView.reloadData()に直接電話することができます。

2)systemLayoutSizeFittingオーバーライド:あなたは(あなたがsurveyArrayを設定した後、レイアウトを調整autoにcell.setNeedsLayout()と交換し、この行cell.do_table_refresh()を削除し、

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)あなたのビューコントローラで:withHorizo​​ntalFittingPriority:verticalFittingPriority機能をannouncementLabelなどの他のUIを更新するメソッドを呼び出す必要があるかもしれません。

関連する問題