我试图弄清楚如何在 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/