我正在尝试在Swift 4中的表中获取所选行。为完整性而提供的代码如下:
import UIKit
import WebKit
class FactorDetailsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var goalTitle: UILabel!
@IBOutlet weak var goalCopy: UITextView!
@IBOutlet weak var goalBenefit: UILabel!
@IBOutlet weak var goalName: UILabel!
@IBOutlet weak var graph: SimpleChart!
@IBOutlet weak var measurement: UILabel!
@IBOutlet weak var measurementRange: UILabel!
@IBOutlet weak var updated: UILabel!
@IBOutlet weak var factorCopy: UITextView!
@IBOutlet weak var impact: UILabel!
@IBOutlet weak var actionsTable: UITableView!
@IBOutlet weak var researchTable: UITableView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var stackView: UIStackView!
var factorData : Factor?
var currentCategory : FactorCategory?
var recommendedActions : [Action] = []
var relatedResearch : [Research] = []
var goal : Goal?
var reading:Double?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = factorData?.factorName
measurement.text = String(reading!)
self.actionsTable.delegate = self
self.actionsTable.dataSource = self
self.actionsTable.isEditing = false
self.researchTable.delegate = self
self.researchTable.dataSource = self
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Update", style: .done, target: self, action: #selector(addStuff))
goalCopy.text = goal?.copy
goalBenefit.text = "BIG BENEFIT"
goalTitle.text = goal?.title
for cat in (factorData?.categories)! {
let s = SimpleChartData(min : Double(cat.min), max : Double(cat.max), label : cat.label, label2 : String(cat.max))
if( graph.canLoad ) {
graph.data!.append(s)
}
}
graph.reading = reading!
// Pin the edges of the stack view to the edges of the scroll view that contains it
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
}
@objc func addStuff() {
// how does this work? Just takes you to the quiz again
let storyboard = UIStoryboard(name: "12Factor", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "AllQuestionViewController") as UIViewController
present(vc, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func configure( factor : Factor )
{
factorData = factor
let mirrored_object = Mirror(reflecting: HRResponses.shared)
reading = 1.0
for (_, attr) in mirrored_object.children.enumerated() {
if let property_name = attr.label as String? {
if factorData?.responseField == property_name {
if let a = attr.value as? String, let aDouble = Double(a) {
reading = aDouble
}
}
}
}
currentCategory = factor.categories.first( where: {$0.min < reading! && $0.max > reading! })
for aind in (currentCategory?.actions)! {
let act = SignalModel.model.actions.first( where: {$0.id == aind})
recommendedActions.append(act!)
}
for rind in (currentCategory?.research)! {
let act = SignalModel.model.research.first( where: {$0.id == rind})
relatedResearch.append(act!)
}
goal = SignalModel.model.goals.first( where: {$0.id == currentCategory?.goals[0]})
}
func tableView(_ tableView: UITableView,
willSelectRowAt indexPath: IndexPath) -> IndexPath? {
print("works?")
return nil
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if tableView == self.actionsTable {
let vc : ActionViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ActionViewController") as! ActionViewController
navigationController?.pushViewController(vc, animated: true)
}
if tableView == self.researchTable {
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of items in the sample data structure.
var count:Int?
if tableView == self.actionsTable {
count = currentCategory?.actions.count
}
if tableView == self.researchTable {
count = currentCategory?.research.count
}
return count!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell?
if tableView == self.actionsTable {
cell = tableView.dequeueReusableCell(withIdentifier: "ActionCell", for: indexPath as IndexPath)
let action = recommendedActions[indexPath.row]
(cell as! ActionCell).configure(a:action)
}
if tableView == self.researchTable {
cell = tableView.dequeueReusableCell(withIdentifier: "ResearchCell", for: indexPath as IndexPath)
let research = relatedResearch[indexPath.row]
(cell as! ResearchCell).configure(r:research)
}
return cell!
}
}
现在,代码太多了。相关部分如下:
self.actionsTable.delegate = self // yes, this is the delegate
self.actionsTable.dataSource = self
self.actionsTable.isEditing = false // no, we're not editing
据我了解,这应该足以在actionsTable中选择触发
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
方法。然而,没有任何反应。调用其他UITableViewDelegate方法,因此该控制器是此表的委托,但是这个方法不会被触发。阅读Apple文档here我看到当表视图处于编辑模式时(即表视图的isEditing属性设置为true)时不调用该方法,但我的表不是在编辑模式下。我的表是否还有其他可能出错的地方,它不允许它向UITableViewDelegate发送事件?我怀疑这与UIScrollView中的表格有关,我读过这不是最佳实践,但是我已经给出了设计,不可妥协地讨论。
答案 0 :(得分:1)
您的问题不是委托,所有代码都很好。您的问题是父滚动视图正在使用水龙头,而不是表格视图。请记住,UITableView
是UIScrollView
的直接子类,因此在滚动视图中放置表视图与在滚动视图中放置滚动视图没有什么不同。 UITableView
内置了所有默认的滚动视图委托,所以只需使用它们。
您不应在UIScrollView中嵌入UIWebView或UITableView对象 对象。如果这样做,可能会因触摸而导致意外行为 这两个对象的事件可以混淆和错误处理。
我知道这不是你想要的答案,因为这不是你的行为,但我个人不会继续进行黑客攻击。我将重构代码并将控制器修剪为一个滚动视图。