直接从 textField 移动到 textField 时,UITableView 不会移动 [英] UITableView doesn't shift when moving from textField to textField directly

查看:21
本文介绍了直接从 textField 移动到 textField 时,UITableView 不会移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力解决我的基于 Swift 的 iPhone 应用程序上的一些 textField 问题的键盘问题.

I'm currently battling with the keyboard covering some textField issue on my Swift based iPhone app.

这张图片显示了主要问题(前三张图片显示了问题,后三张是应该做的):

This image shows the primary problem (top three images show the problem, bottom three are what it should be doing):

当编辑 tableViewCell 中的 textField 时,我想向上移动整个 tableview 和导航栏.我可以用下面的代码(取自 viewController 的片段)来做到这一点:

When the textField in the tableViewCell is edited I want to move the whole tableview and navigation bar up. I can do this with the code below (snippets taken from the viewController):

var tableViewShiftedUp = false

...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! QuantityTableViewCell

        let componentLocationQuantity = tableViewData[indexPath.row]

        if let locationString = componentLocationQuantity.location.valueForKey("location_name") as? String {
            cell.setCellContents(locationString, quantity: componentLocationQuantity.quantity.floatValue)
            cell.quantityLabel.delegate = self
        }

        //get stock level related to current build rate and lead time (good - green, warning - orange, urgent - red)
        let status = model.determineStockLevelStatus(component)
        cell.setQuantityLabelBackgroundStatusColor(status)

        if editingMode == true {
            cell.makeQuantityEditable()
        }

        //add control event to detect text change
        cell.quantityLabel.addTarget(self, action: #selector(quantityCellTextChanged(_:)), forControlEvents: UIControlEvents.EditingChanged)
        cell.quantityLabel.addTarget(self, action: #selector(quantityCellSelected(_:)), forControlEvents: UIControlEvents.EditingDidBegin)


        return cell
    }

...

//MARK: - UITextfield delegate
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)

        if tableViewShiftedUp == true {
            moveTable()
        }

    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    func quantityCellSelected(textField: UITextField){
        if tableViewShiftedUp == false {
            moveTable()
        }
        //check table was moved
        print(tableView.frame.origin.y)
    }

    func hideKeyboard(){
        self.view.endEditing(true)
        if tableViewShiftedUp == true {
            moveTable()
        }
    }

    func moveTable(){
        if tableViewShiftedUp == true {
            animateTableViewMoving(false, moveValue: 250)
            animateNavigationBarMoving(false, moveValue: 250)
            tableViewShiftedUp = false
        } else {
            animateTableViewMoving(true, moveValue: 250)
            animateNavigationBarMoving(true, moveValue: 250)
            tableViewShiftedUp = true
        }
    }

    // moving the tableView
    func animateTableViewMoving (up:Bool, moveValue :CGFloat){
        let movementDuration:NSTimeInterval = 0.0
        let movement:CGFloat = ( up ? -moveValue : moveValue)
        UIView.beginAnimations( "animateView", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration )
        self.tableView.frame = CGRectOffset(self.tableView.frame, 0,  movement)
        UIView.commitAnimations()
    }
    // moving the navigationBar
    func animateNavigationBarMoving (up:Bool, moveValue :CGFloat){
        let movementDuration:NSTimeInterval = 0.0
        let movement:CGFloat = ( up ? -moveValue : moveValue)
        UIView.beginAnimations( "animateView", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration )
        self.midNavigationBar.frame = CGRectOffset(self.midNavigationBar.frame, 0,  movement)
        UIView.commitAnimations()
    }

当直接移动到 tableView 中的 textField 时它工作正常.但是,当我已经在 tableView 之外编辑 textField 时,不会发生向上移动,因此移动切换不同步.

And it works fine when moving directly to the textField in the tableView. However, when I am already editing a textField outside the tableView the shift up doesn't happen and so the shifting toggle gets out of sync.

我已经打印了 tableView 框架原点:

I've printed the tableView frame origin:

//check table was moved
print(tableView.frame.origin.y)

所以我可以看到 tableView 的设置以及从 tableView 外部的 textField 到 tableView 内部的 textField 的第一次移动,这个属性是我期望的 134,但是它仍然在屏幕上的 384下次调用时会打印出来.

so I can see what the tableView is set to and on that first move from textField outside the tableView to textField inside the tableView, this property is what I would expect it to be 134, however it's still at 384 on the screen which it prints the next time it's called.

在 tableView 中的单元格内移动时不会出现此问题.

The problem doesn't occur when moving within cells in the tableView.

感觉就像我遇到了某种竞争条件问题,或者我错过了一些被调用的委托方法,该方法在从一个单元格转换到下一个单元格时会抛出所有内容.

It feels like I've got some kind of race condition problem or I'm missing some delegate method being called that is throwing everything off when transitioning from one cell to the next.

帮助会很棒.

推荐答案

查看这个库:https://github.com/michaeltyson/TPKeyboardAvoiding.设计使键盘不受文本字段的影响.

Check out this library: https://github.com/michaeltyson/TPKeyboardAvoiding. Designed so that it gets the keyboard out of the way of text fields.

这篇关于直接从 textField 移动到 textField 时,UITableView 不会移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆