【发布时间】:2017-07-04 10:24:53
【问题描述】:
我有一个自定义 UITableView 和一个带有 UISegmentedControl 的自定义 UITableViewCell。该单元格位于表格的第一行内。
问题是我正在使用 valueChanged 事件从 json 加载数据,当我重新加载 tableview 时,选择索引会自动返回到第一段。我该如何解决这个问题?
这是我的代码的一部分:
@IBAction func segmentChanged(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex{
case 0:
loadIdeas(idRole:[0,1], id_user: 1, estado: "todas")
case 1:
loadIdeas(idRole:[0,1], id_user: 1, estado: "aceptada")
case 2:
loadIdeas(idRole:[0,1], id_user: 1, estado: "pendiente")
case 3:
loadIdeas(idRole:[0,1], id_user: 1, estado: "rechazada")
default:
loadIdeas(idRole:[0,1], id_user: 1, estado: "todas")
}
}
我在“loadIdeas”函数中刷新数据,如下所示:
DispatchQueue.main.async{
self.table.tableView.reloadData() // Refrescamos el contenido de la tabla
}
每次我选择另一个段时,数据都会正确更改,但分段控件总是在重新加载数据之前保持第一个段被选中。
请帮帮我。我已经为此浪费了几个小时的时间,但在网络上没有发现任何可以帮助我的类似情况
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(cells[indexPath.row].cell == 1){
let cell = Bundle.main.loadNibNamed("CustomHeaderCell", owner: self, options: nil)?.first as! CustomHeaderCell
cell.menuLabel.text = cells[indexPath.row].titulo
return cell
}
else if(cells[indexPath.row].cell == 2){
let cell = Bundle.main.loadNibNamed("SegmentedControlCell", owner: self, options: nil)?.first as! SegmentedControlCell
cell.table = self
return cell
}
else{
let cell = Bundle.main.loadNibNamed("MyTableViewCell", owner: self, options: nil)?.first as! MyTableViewCell
cell.labelTitulo.text = cells[indexPath.row].titulo
cell.labelDescripcion.text = cells[indexPath.row].descripcion
cell.labelEstado.text = cells[indexPath.row].estado
cell.labelFecha.text = cells[indexPath.row].fecha
cell.labelDepartamento.text = cells[indexPath.row].departamento
//cell.imageView?.image = cells[indexPath.row].imagen
return cell
}
}
【问题讨论】:
-
你能分享你的`tableView(_:cellForRowAt:) 方法吗?
-
我是 iOS swift 新手,所以我的代码可能是一场灾难:
-
抱歉,我已经用我的那部分代码更新了第一条评论
-
太棒了!每个人都从同一个地方开始。 ???如果我的回答有道理,请告诉我。
标签: ios swift uitableview swift3 uisegmentedcontrol