У меня есть следующая структура, отвечающая за декодирование данных JSON, используемых для представления табличного представления.Переменная isSelected отвечает за отслеживание того, какие строки выделены или отменены.
Проблема, с которой я сталкиваюсь, заключается в том, что при отмене выбора строки необходимо дважды нажать для снятия флажка.Почему это происходит?
struct Portfolio: Decodable {
let person: String
let code: String
var tick: Int
var isSelected : Bool {
get { return tick == 2 }
set { tick = newValue ? 2 : 1 }
}
enum CodingKeys : String, CodingKey {
case person, code, tick
}
}
The following function is responsible for either adding or removing a checkmark after updating the record in the database using either the selected or unselected url.
func updateSelection(of portfolio: Portfolio, at indexPath: IndexPath) {
let url = portfolio.isSelected ? "https://example.com/example/selected" : "https://example.com/example/unselected"
let parameters: Parameters = [portfolio.code: portfolio.tick]
Alamofire.request(url, method: .post, parameters: parameters).responseString { response in
switch response.result {
case .success(let string): self.tableView.reloadRows(at: [indexPath], with: .none)
case .failure(let error): print(error)
}
}
}
В моем методе didSelectRowAt содержится следующее:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
sections[indexPath.section].items[indexPath.row].isSelected.toggle()
let portfolio = sections[indexPath.section].items[indexPath.row]
updateSelection(of: portfolio, at: indexPath)
}
В моем методе cellForRowAt содержится следующее:
let item = sections[indexPath.section].items[indexPath.row]
cell.accessoryType = item.isSelected ? .checkmark : .none