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


按照书上做的一个例子,在点击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

uitableviewcell swift

Amiko 10 years, 2 months ago

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

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

G弦上的板砖 answered 10 years, 2 months ago

Your Answer