【问题标题】:SWIFT TableView not respondingSWIFT TableView 没有响应
【发布时间】:2015-09-14 21:04:08
【问题描述】:

我有一个问题,我有一个控制器,在 viewdidload 中,我尝试加载从 xib 文件创建的子视图。

我的“自定义”子视图已很好地添加到我的第一个控制器中,但 tableview 没有响应......我的意思是它不会滚动,当我点击它时,什么也没有发生......(我还没有实现当单击单元格时触发动作的方法,但单击时单元格未突出显示)。

这里是代码:



class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let v1 = MyViewTest(frame: CGRectZero)

        v1.tableView.dataSource = self
        v1.tableView.delegate = self

        self.view.addSubview(v1)


    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //datasource method returning the what cell contains
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
        //reusing the previous scrolled cells from table view(if any)
        //cell.textLabel?.text = array[indexPath.row]
        cell.textLabel?.text = "test"
        //passing each elements of the array to cell
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //datasource method returning no. of rows
        return 14
    }


}

这里是 MyViewTest 中的代码



class MyViewTest: UIView {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!


    var view:UIView!

    let nibName:String = "MyViewTest"

    override init(frame: CGRect) {
        super.init(frame:frame)
        setup()
    }

    required init(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setup()
    }


    func setup() {
        view = loadViewFromNib()

        let screenSize: CGRect = UIScreen.mainScreen().bounds
        let sWidth = screenSize.width
        let sHeight = screenSize.height
        let xPos = sWidth/2-(view.bounds.width/2)
        let yPos = sHeight/2-(view.bounds.height/2)

        view.frame = CGRectMake(xPos, yPos, view.bounds.width, view.bounds.height)

        addSubview(view)
    }

    func loadViewFromNib () -> UIView {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let mview = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

        return mview
    }


}

在 xib 文件中,我只有一个带有标签的视图和我的 tableview。xib 文件与其类相关联(在标识符中)。

如果您知道为什么我的表格视图不起作用,那就太好了!

【问题讨论】:

  • 看起来 v1 可能会受到所有的影响,因为它位于 tableview 的顶部。将 v1 的背景设置为某种颜色(或使用视图调试器)并检查。
  • 我添加了我的 xib 文件的打印屏幕,我已经有了 v1 的颜色...
  • 您似乎将 MyTestView 的框架设置为 CGRectZero。然后,您将表格添加为该视图的子视图,并设置框架大小。由于 MyTestView 的宽度和高度为 0,并且视图的默认设置是不剪辑子视图,我想您可以看到表格但不能单击它。尝试将 MyTestView 的框架设置为屏幕大小?
  • 确实,我也在苹果开发者论坛上发布了我的问题,这正是我得到的答案,你是对的。我来这里是为了写解决方案,但你已经给了它。无论如何,谢谢你回答我,因为我对此非常绝望,你的回答会对我有所帮助!
  • 很高兴为您提供帮助。我发布了答案。

标签: ios iphone swift uitableview subview


【解决方案1】:

您似乎将 MyTestView 的框架设置为 CGRectZero。

然后,您将表格添加为该视图的子视图,并设置了框架大小。由于 MyTestView 的宽度和高度为 0,并且视图的默认设置是不剪辑子视图,我想您可以看到表格但不能单击它。

尝试将 MyTestView 的框架设置为屏幕大小?

【讨论】:

    【解决方案2】:

    出现问题是因为我使用 CGRectZero 实例化了 MyViewTest。例如,如果我使用具有实际宽度和高度的 CGRectMake,则效果很好。

    有关更多详细信息,请参阅上面的 rory mckinnel 帖子 :-)

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多