UITableViewCell没有收到任何接触

时间:2016-08-16 23:46:03

标签: ios swift uitableview autocomplete autolayout

我正在实现一个显示大量信息的视图。视图是可滚动的,在视图内部我实现了一个不可滚动的表视图,用于保存用户注释。我有所有自动布局约束,它似乎正确布局,但在某一行下面没有接收到触摸。看来桌面视图或其他东西阻止了下面的视图接收事件,但我无法追查这个问题。

我希望主滚动视图的内容大小随着评论表视图的增长而增长。将帖子评论视图保留在表格视图的底部。现在我无法选择最后一个单元格或文本字段。

评论单元格视图 Comment Cell View

模拟器截图 Simulator screenshot

以下是表视图实现的代码:

    commentsTableView.delegate = self
    commentsTableView.dataSource = self
    commentsTableView.estimatedRowHeight = 82
    commentsTableView.rowHeight = UITableViewAutomaticDimension
    commentsTableView.sectionHeaderHeight = UITableViewAutomaticDimension
    commentsTableView.estimatedSectionHeaderHeight = 54
    commentsTableView.estimatedSectionFooterHeight = 0
    commentsTableView.sectionHeaderHeight = UITableViewAutomaticDimension
    commentsTableView.tableFooterView = UIView(frame: CGRectZero)


 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Comments"
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return comments.count
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section != 0 { return nil }

    let sectionTitle: String = self.tableView(tableView, titleForHeaderInSection: section)!
    if sectionTitle == "" {
        return nil
    }

    let view = UIView(frame: CGRectMake(0, 0, tableView.frame.width, 54))
    let title = UILabel(frame: CGRectMake(60, 22, tableView.frame.width, 17))
    view.addSubview(title)

    title.text = sectionTitle
    title.textColor = UIColor(red: (74 / 255), green: (74 / 255), blue: (74 / 255), alpha: 1.0)
    title.backgroundColor = UIColor.clearColor()
    view.backgroundColor = UIColor.clearColor()
    title.font = UIFont(name: "ProximaNova-Semibold", size: 16.0)

    view.layer.addBorder(.Bottom, color: UIColor.lightGrayColor().colorWithAlphaComponent(0.75), thickness: 0.5)
    title.setNeedsDisplay()
    view.setNeedsDisplay()

    return view
}

2 个答案:

答案 0 :(得分:0)

您必须将userInteractionEnabled设置为true才能触发这些事件。

view.userInteractionEnabled = true

答案 1 :(得分:0)

Comment TextField是注释TableView的最后一行。因此,将注释TextField代码放在TableView的页脚中,如下所示:

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let commentTextFieldView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 50))

       // Your Comment TextField code

        return commentTextFieldView
    }

     func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 50.0
    }

还有另一种方法可以将Comment TextField视图指定为tableView页脚:

myTableView.tableFooterView = commentTextFieldView