【发布时间】:2017-01-01 20:56:47
【问题描述】:
我在将 tableview 放入 uitableview 单元格时遇到问题。现在,它甚至不显示嵌套的 tableview。我将 tableview 的委托和数据源设置为自定义单元格,并拥有所有必需的方法。我还在为我的表格视图使用故事板,并正确连接了所有插座。
问题在于它没有进入 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.
}
}
下面是保存自定义单元格(包含嵌套表格视图)的视图控制器中的代码的 sn-p。此代码是 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。现在,它甚至不显示嵌套的 tableview。
【问题讨论】:
标签: swift uitableview nested