在iOS 10.x
下一切正常我在应用程序中有一个表格,该表应该扫描可用的BLE设备并将每个设备显示在单元格中。
当用户点击单元格时,它应该更新textlabel.text属性,以说"请等待..."。连接后,它应调用tableview.reload data()方法,并且单元格应列出设备名称。如果连接了设备的状态,则应说连接。
更新iOS 11后,一切似乎都在起作用,但是我无法更新单元格中的文本以表示"连接"。它保留在细胞时显示"请等待"消息。
查看控制器类定义...
class BeanConnect: UIViewController, UITableViewDelegate, PTDBeanManagerDelegate, PTDBeanDelegate {
@IBOutlet var tableView: UITableView!
let beanList=BeanList()
var beanManager: PTDBeanManager?
var waitingToConnect: PTDBean?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.register(UITableViewCell.self,forCellReuseIdentifier: "Cell")
tableView.delegate=self
tableView.dataSource=beanList
beanManager = PTDBeanManager()
beanManager!.delegate = self
//tableView.reloadData()
}
这是我在触摸单元格时更新文本标签的地方。这很好。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell=tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath as IndexPath)
cell.textLabel?.text=beanList.items[indexPath.row].name + " Please wait..."
if (myBean != nil && myBean?.state==PTDBleDeviceState.connectedAndValidated) {
let waitingBean=beanList.items[indexPath.row]
self.waitingToConnect=waitingBean
var e: NSErrorPointer
print("*******now disconnecting from bean********")
beanManager?.disconnect(fromAllBeans: e)
}else {
myBean=beanList.items[indexPath.row]
connectToBean(bean: myBean!)
}
}
这是更新单元格的部分(在单独的模块中)。请注意,调试线表明"连接和验证"正在触发。除了该死的标签没有更新外,一切似乎都起作用。
extension BeanList: UITableViewDataSource {
@available(iOS 2.0, *)
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath as IndexPath)
let item=items[indexPath.row]
print("debug code building the table")
switch item.state {
case PTDBleDeviceState.attemptingConnection:
cell.textLabel!.text=item.name + " Connecting..."
print("debug code attemptingConnection")
case PTDBleDeviceState.attemptingValidation:
cell.textLabel!.text=item.name + " Validating..."
print("debug code attemptingValidation")
case PTDBleDeviceState.connectedAndValidated:
cell.textLabel!.text=item.name + " Connected!"
print("debug code Connected")
print(item.name + " Connected!")
default:
cell.textLabel!.text=item.name
print("debug code default case")
}
return cell
}
任何帮助将不胜感激!TX
您不应在" didSelect"中排出新单元格。使用tableview.cellforrow并获取现有单元格。