Swift MPGTextField 在表格视图单元格中自动完成

Swift MPGTextField autocomplete in tableview cell

我在输入时看不到建议..我有表格视图单元格和文本字段。

我正在使用 MPGTextField library, swift version(支持 swift 2)。

有什么解决办法吗?

代码:

@IBOutlet weak var articleField: MPGTextField_Swift!

override func viewDidLoad() {
    super.viewDidLoad()
    articleField.mDelegate = self
}

func dataForPopoverInTextField(textfield: MPGTextField_Swift) -> [Dictionary<String, AnyObject>] {
    return articles
}

func textFieldShouldSelect(textField: MPGTextField_Swift) -> Bool{
    return true
}

func textFieldDidEndEditing(textField: MPGTextField_Swift, withSelection data: Dictionary<String,AnyObject>){
    print(data["CustomObject"])
}

我真的不知道发生了什么。自动完成框是否显示但在 tableViewCell 的底部被截断了?如果是这样,请尝试在 tableViewCell 上将 clipsToBounds 设置为 false,甚至将其内容视图也设置为 false。

默认情况下,视图框架之外的区域不会识别触摸事件。要将点击路由到建议,您必须继承 tableViewCell 并覆盖 hitTest

此问题的解决方案是在选择单元格时设置单元格高度:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if tableView.indexPathForSelectedRow == indexPath {
        return self.selectedCellHeight
    }

    return self.unSelectedCellHeight
}

您只需设置 unselectedCellHeightselectedCellHeight

beginUpdates()endUpdates() 将更新单元格高度并动画化它。

MPGTextField-Swift.swift 中你会找到一个函数 provideSuggestions()
在此函数中,您会找到一行

self.superview!.addSubview(tableViewController!.tableView)

将此行替换为

//BUG FIX - SHOW ON TOP

//self.superview!.addSubview(tableViewController!.tableView)

let aView = tableViewController!.tableView
var frame = aView.frame

frame.origin = self.superview!.convertPoint(frame.origin, toView: nil)
aView.frame = frame

self.window!.addSubview(aView)

////

我已经 fork MPGTextField 存储库,为了演示目的进行了必要的更改。
你可以在 https://github.com/rishi420/MPGTextField 找到我的仓库

注意:这个repo需要Xcode 7.1.1来编译。随意贡献。 :-]