SWIFT TableView 没有响应

Posted

技术标签:

【中文标题】SWIFT TableView 没有响应【英文标题】:SWIFT TableView not responding 【发布时间】: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 的框架设置为屏幕大小? 确实,我也在苹果开发者论坛上发布了我的问题,这正是我得到的答案,你是对的。我来这里是为了写解决方案,但你已经给了它。无论如何,谢谢你回答我,因为我对此非常绝望,你的回答会对我有所帮助! 很高兴为您提供帮助。我发布了答案。 【参考方案1】:

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

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

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

【讨论】:

【参考方案2】:

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

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

【讨论】:

以上是关于SWIFT TableView 没有响应的主要内容,如果未能解决你的问题,请参考以下文章

Tableview 行没有在 swift 中显示带有附加值的顺序

如何在iOS swift的tableview中使用json响应中的键和值?

TableView 没有响应 UINavigationController 中的“编辑”按钮

iphone:SDK 披露按钮没有从第一个 TableView 响应到第二个详细 TableView?

tableView 编辑模式删除按钮(减号)没有响应点击 - 我该如何解决这个问题?

如何在swift 3中重新加载没有动画的特定单元格?