ios - UITableView 静态第一个单元格与 Google SDK

标签 ios swift uitableview google-maps

我试图弄清楚如何在 UITableView 中拥有静态的第一个单元格,然后为其余单元格提供动态结果。

更具体地说,我正在使用 Google SDK,当用户在 UISearchBar 中输入地址或位置时,就会在下面的 UITableView 中自动完成结果。我希望搜索栏下的第一个单元格保持静态并显示“当前位置”,然后显示其下方的自动完成结果。

我尝试使用类似于 here 中概述的方法。我不知道如何让它工作,因为这使得从谷歌自动完成返回的索引超出范围。

这是我一直在尝试但不起作用的一般方法:

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.searchResultsGoogle.count + 1
}

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

    //let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath)

    if indexPath.row == 0 {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath)
        cell.textLabel?.text = "Current Location"
        cell.textLabel?.textColor = UIColor(red: 14/255, green: 122/255, blue: 254/255, alpha: 1)
        return cell

    }
    else {

        let cell1 = self.tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath)
        cell1.textLabel?.text = self.searchResultsGoogle[indexPath.row]
        return cell1
    }

}

关于如何解决这个问题有什么想法吗?谢谢!

最佳答案

更改此:

cell1.textLabel?.text = self.searchResultsGoogle[indexPath.row]

至:

cell1.textLabel?.text = self.searchResultsGoogle[indexPath.row - 1]

这将调整行索引以考虑固定行。

更好的解决方案可能是使用单独的部分。将固定行放入第 0 部分,将实际数据放入第 1 部分。

顺便说一句 - 由于单元重复使用,您还遇到了另一个问题。您需要重置其他行的文本颜色。否则,当您滚动表格 View 时,某些行将显示错误的颜色。

关于ios - UITableView 静态第一个单元格与 Google SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852708/

相关文章:

ios - SVProgressHUD - 快速显示/隐藏控件会导致它在下次调用 showWithStatus 时不出现

ios - 当我回到包含 ARSCNView 的 View Controller 时,相机卡住。如何在 ARSCNView 停止工作的情况下进入和离开这个 View Controller ?

ios - 如何使单元格不与其他单元格一起滚动?

ios - 在运行时在表格 View 单元格中添加 subview 并展开表格 View 单元格的最佳方法

ios - 不同的 Xcode 项目进行修订

iOS Firebase - 在模拟器中崩溃但不是设备 w/persistence

ios - NSPredicate 通过数组中的字符串值

arrays - 在 Swift 中订购二维数组

objective-c - 编辑 UITableView 时如何防止删除按钮弹出?

ios - 在多个 bundle ID 中使用应用内购买项目