请问static的tableViewCell不响应点击会是哪里出了问题?

发布于 2022-09-01 12:10:18 字数 6220 浏览 42 评论 0

按照书上做的一个例子,在点击Due Date的时候底下应该出现一个datePicker

clipboard.png

像这样

clipboard.png

运行没有报错,但是cell点击没有反应.

代码如下:

import UIKit

protocol ItemDetailViewControllerDelegate: class {
    func itemDetailViewControllerDidCancel(controller:ItemDetailViewController)
    func itemDetailViewController(controller:ItemDetailViewController,didFinishAddingItem item:CheckListItem)
    func itemDetailViewController(controller:ItemDetailViewController,didFinishEditingItem item:CheckListItem)
}

class ItemDetailViewController: UITableViewController, UITextFieldDelegate {

    var itemToEdit:CheckListItem?

    var dueDate = NSDate()

    var datePickerVisible = false  //如果直接改成true能显示,证明cell是正确配置的,但是duedate cell不知为何无法响应点击

    weak var delegate: ItemDetailViewControllerDelegate?

    @IBOutlet weak var textField: UITextField!

    @IBOutlet weak var doneBarButton: UIBarButtonItem!

    @IBOutlet weak var shouldRemindSwitch: UISwitch!

    @IBOutlet weak var dueDateLable: UILabel!

    @IBAction func Cancel(sender: UIBarButtonItem) {

        delegate?.itemDetailViewControllerDidCancel(self)
    }


    @IBAction func done() {

        if let item = itemToEdit {

        item.text = textField.text
        item.shouldRemind = shouldRemindSwitch.on
        item.dueDate = dueDate

        delegate?.itemDetailViewController(self, didFinishEditingItem: item)

        }else {

            let item = CheckListItem()
            item.text = textField.text
            item.checked = false
            item.shouldRemind = shouldRemindSwitch.on
            item.dueDate = dueDate

            delegate?.itemDetailViewController(self, didFinishAddingItem: item)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = 44

        if let item = itemToEdit {

            title = "Edit Item"
            textField.text = item.text
            doneBarButton.enabled = true
            shouldRemindSwitch.on = item.shouldRemind
            dueDate = item.dueDate
        }
        updateDueDateLable()
    }

    override func viewWillAppear(animated: Bool) {          super.viewWillAppear(animated)
        textField.becomeFirstResponder()
    }

    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {  //使duedate可以点击

        if indexPath.section == 1 && indexPath.row == 1 {
            return indexPath
    } else {
        return nil
        }
    }

    func showDatePicker() {
            datePickerVisible = true //显示datepicker

            let indexPathDatePicker = NSIndexPath(forRow: 2, inSection: 1)

            tableView.insertRowsAtIndexPaths([indexPathDatePicker], withRowAnimation: .Fade) //在section1的row2(第三行)插入datepicker
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

                if section == 1 && datePickerVisible {  //满足条件的话section1 有3行
        return 3
    }else {
        return super.tableView(tableView, numberOfRowsInSection: section)
                }
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {  //手动配置section1的row2(第三行)

        if indexPath.section == 1 && indexPath.row == 2 {

            var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("DatePickerCell") as? UITableViewCell

            if cell == nil {
                cell = UITableViewCell(style: .Default,
                             reuseIdentifier: "DatePickerCell")
                cell.selectionStyle = .None


            let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 320, height: 216))

            datePicker.tag = 100

            cell.contentView.addSubview(datePicker) //在cell上加入datepicker

            datePicker.addTarget(self, action: Selector("dateChanged:"),
                             forControlEvents: .ValueChanged)
        }
             return cell

    }else {

        return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
        }
    }


    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.section == 1 && indexPath.row == 2 {
            return 217
        }else {
                return super.tableView(tableView,
        heightForRowAtIndexPath: indexPath)
        }
    }

    override func tableView(tableView: UITableView, var indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {

        if indexPath.section == 1 && indexPath.row == 2{
            indexPath = NSIndexPath(forRow: 0, inSection: indexPath.section)
        }
        return super.tableView(tableView, indentationLevelForRowAtIndexPath:indexPath)
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

        let oldText:NSString = textField.text
        let newText:NSString = oldText.stringByReplacingCharactersInRange(range, withString: string)

        doneBarButton.enabled = (newText.length > 0)

        return true
    }

    func updateDueDateLable() {
        let formatter = NSDateFormatter()
        formatter.dateStyle = NSDateFormatterStyle.MediumStyle
        formatter.timeStyle = NSDateFormatterStyle.ShortStyle
        dueDateLable.text = formatter.stringFromDate(dueDate)
    }

    override func tableView(tableView: UITableView,
            didSelectRowAtIndexPath indexPath: NSIndexPath) {

            tableView.deselectRowAtIndexPath(indexPath, animated: true)
            textField.resignFirstResponder()

            if indexPath.section == 1 && indexPath.row == 1 { //点击section1的row1(第二行 duedate)显示datepicker
            showDatePicker()
            }
    }
}

这是Storyboard
clipboard.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2022-09-08 12:10:18

教程的作者很耐心地帮我看了project,http://www.raywenderlich.com/forums/viewtopic.php?f=11&t=22199

我不知道什么时候把这个地方选成no selection了,图片描述改过来问题解决了.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文