gpt4 book ai didi

swift - 将 Tableview 放入 UITableViewCell Swift

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:44 25 4
gpt4 key购买 nike

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

问题是它没有进入 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.

}
}

下面是保存自定义单元格(包含嵌套表格 View )的 View Controller 中的一段代码。此代码是 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。现在,它甚至不显示嵌套的表格 View 。

最佳答案

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) 在你的 View Controller 中,在你设置了surveyArray之后,删除这一行cell.do_table_refresh(),替换为cell.setNeedsLayout() 自动调整布局(您可能还需要调用一个方法来更新其他 UI,例如 announcementLabel 等。在 setNeedsLayout 之前调用它。)

关于swift - 将 Tableview 放入 UITableViewCell Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41418964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com