如何在编辑uitextfield时停止在uitableview中滚动

时间:2017-04-05 12:06:00

标签: ios swift xcode uitableview uitextfield

  

Stack Overflow Solution。当我使用super. viewWillAppear(true)时,我的视图看起来很完美,但是这里自动滚动会导致编辑UITextField时出现问题。

在我的UITableView我使用两个cell一个用于详细信息而另一个用于页脚按钮Sign UP

  

在KeyBoard之前查看图像

BEFORE KeyBoard

  

在KeyBoard之后查看图像

After KeyBoard

此处为页脚视图代码

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    self.navigationController?.isNavigationBarHidden = false
    getTimeZone()
    spinnerInitialization()
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
    imageView.contentMode = .scaleAspectFit
    let image = UIImage(named: "Home_Logo2")
    imageView.image = image
    navTitle.titleView = UIImageView(image: image)
    hideKeyboardWhenTappedAround()
}
  

代码页脚,您可以在其中看到注册按钮

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let  footerCell = tableView.dequeueReusableCell(withIdentifier: "footer") as! CreateAccountTableViewCell
    let indexPath = IndexPath(row: 0, section: 0)
    let  cell = tableView.cellForRow(at: indexPath) as! CreateAccountTableViewCell
    footerCell.signupButtonClick = {
        if cell.cmpName.text!.isEmpty || cell.email.text!.isEmpty || cell.firstName.text!.isEmpty || cell.lastName.text!.isEmpty || cell.password.text!.isEmpty || cell.cnfirmPassword.text!.isEmpty  {
            let dialog = UIAlertController(title: "All Field Required", message: "Please check, All field are required", preferredStyle: UIAlertControllerStyle.alert);
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){(ACTION) in
                return
            }
            dialog.addAction(okAction);
            DispatchQueue.main.async(execute: {
                UIApplication.shared.keyWindow?.rootViewController?.present(dialog, animated: true, completion: nil)
            })
        }
        else {
            if cell.password.text! != cell.cnfirmPassword.text! {
                let dialog = UIAlertController(title: "Confirm Password does not matched.", message: "", preferredStyle: UIAlertControllerStyle.alert);
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){(ACTION) in
                    return
                }
                dialog.addAction(okAction);
                DispatchQueue.main.async(execute: {
                    UIApplication.shared.keyWindow?.rootViewController?.present(dialog, animated: true, completion: nil)
                })
            }
            let postString = "cmpname=\(cell.cmpName.text!)&email=\(cell.email.text!)&firstName=\(cell.firstName.text!)&lastName=\(cell.lastName.text!)&password=\(cell.password.text!)&cnfirmPassword=\(cell.cnfirmPassword.text!)&token=\("afdg2015")&signUpFrom=\("IOS")&timezone=\(cell.timezonePickerView.selectedRow(inComponent: 0))&currentPlanId=\(4)"
            self.registerMe(postString: postString, password: cell.password.text! )
        }
    }
    return footerCell
}
  

详细信息视图查看编辑文本的位置

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "detailsCell", for: indexPath) as! CreateAccountTableViewCell
    DispatchQueue.main.async(execute: {
        cell.timezonePickerView.reloadAllComponents()
        cell.timezonePickerView.selectRow(self.idSelect, inComponent: 0, animated: false)
        cell.cmpName.underlined()
        cell.email.underlined()
        cell.firstName.underlined()
        cell.lastName.underlined()
        cell.password.underlined()
        cell.cnfirmPassword.underlined()
        cell.cmpName.delegate = self
        cell.email.delegate = self
        cell.firstName.delegate = self
        cell.lastName.delegate = self
        cell.password.delegate = self
        cell.cnfirmPassword.delegate = self
    })
    cell.selectionStyle = .none
    return cell
}

这时当我点击UITextField时,视图会自动滚动到任意位置。我希望滚动应该在用户滚动时工作,其他滚动应该停止滚动。

我如何才能完成这项任务......

1 个答案:

答案 0 :(得分:0)

  1. 使用常规UIViewController代替UITableViewController
  2. 将您的UITableView添加为主视图的子视图。
  3. 添加SIGN UP按钮作为主视图的另一个子视图,约束在主视图的底部。
  4. 根据需要添加键盘WillShow / DidShow / WillHide / DidHide通知的处理。当键盘滑入或滑出视图时,可同时为注册按钮视图的位置设置更改动画。
  5. 这将禁用表格视图的自动滚动。