首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在didSelectRowAt中触发UITableViewDelegate

无法在didSelectRowAt中触发UITableViewDelegate
EN

Stack Overflow用户
提问于 2018-05-11 23:29:09
回答 1查看 139关注 0票数 0

我试图在Swift 4中的一个表中获取所选的行。

代码语言:javascript
复制
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!
}

}

现在,代码太多了。有关部分如下:

代码语言:javascript
复制
self.actionsTable.delegate = self // yes, this is the delegate
self.actionsTable.dataSource = self
self.actionsTable.isEditing = false // no, we're not editing

据我所知,这应该足以使actionsTable中的选择触发

代码语言:javascript
复制
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

方法。然而,什么都没有发生。调用其他UITableViewDelegate方法,因此此控制器是此表的委托,但是这一方法从未被触发。通过阅读Apple 这里,我发现当表视图处于编辑模式(即表视图的isEditing属性设置为true)时,不会调用该方法,但我的表没有处于编辑模式。我的表是否还有其他不允许将事件发送到UITableViewDelegate的错误?我怀疑这与UIScrollView中的表有关,我已经读过这不是最佳实践,但与我所得到的设计是无可争辩的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-12 02:25:31

您的问题不是委托,所有这些代码都很好。您的问题是父滚动视图使用的是抽头,而不是表视图。请记住,UITableViewUIScrollView的直接子类,因此在滚动视图中放置表视图与将滚动视图放置在滚动视图中没有什么不同。UITableView内置了所有默认滚动视图委托,所以只需使用这些。

不应将UIWebView或UITableView对象嵌入到UIScrollView对象中。如果这样做,可能会导致意外行为,因为这两个对象的触摸事件可能会被混淆和错误处理。

苹果dox

我知道这不是你想要的答案,因为这不是你做的,但我个人不会继续进行黑客攻击。我将重构代码并将控制器缩减为一个滚动视图。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50301478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档