У меня проблема с моими ячейками, когда я удаляю / вставляю свои tableView
, я удаляю и вставляю свои ячейки так:
self.tableView.beginUpdates()
user.lobbySurvey.remove(at: 0)
self.tableView.deleteRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
self.tableView.endUpdates()
вставка:
self.tableView.beginUpdates()
user.lobbySurvey.insert(surveyEnded, at: rowToInsert)
self.tableView.insertRows(at: [IndexPath(row: rowToInsert, section: 0)], with: .fade)
self.tableView.endUpdates()
lobbySurvey
- массив данных TableView.
Проблема в том, что добавляемая ячейка сохраняет дизайн первой ячейки, которую я удалила ранее.
Я думаю, что это проблема, потому что я проверяю, взята ли карта или нет. Если не отмечен: вид карты добавляется каждый раз при повторном использовании ячейки.
вот как я проверяю, нарисована ли ячейка или нет:
func drawSurveyEnded(){
if(cardIsDraw == false){
surveyEnded.draw(cardView: self.cardView)
surveyEnded.delegate = self
self.addCardShadow()
cardIsDraw = true
}
}
Функция вызывается много раз, вызывая повторное использование системы iOS. Поэтому я думаю, что моя проблема возникла отсюда.
Почему моя ячейка в моем tableView сохраняет дизайн предыдущей ячейки, которую я удалил?
Благодаря вашему ответу
Другой код:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"Celll") as! CardCell
for survey in user.lobbySurvey{
let index = user.lobbySurvey.index(where: {
//get the current index is nedeed else the cells reuse lazy
$0 === survey
})
if indexPath.row == index{
var surveyState : UserSurvey.state
surveyState = survey.state
switch surveyState{
case .selectSurvey:
cell.drawCard(statutOfCard: .selectSurvey)
case .goSurvey:
cell.drawCard(statutOfCard: .goSurvey(picture: survey.picture))
case .surveyEnded:
cell.drawCard(statutOfCard: .surveyEnded(picture: survey.picture))
case .surveyWork:
print("survey in progress to vote")
case .surveyWaiting:
cell.drawCard(statutOfCard: .surveyWaiting(selfSurveyId: survey.id, timeLeft: survey.timeLeft, picture: survey.picture))
case .buyStack:
cell.drawCard(statutOfCard: .buyStack(supView : self.view))
}
}
}
cell.delegate = self
cell.delegateCard = self
cell.layer.backgroundColor = UIColor.clear.cgColor
cell.backgroundColor = .clear
tableView.backgroundColor = .clear
tableView.layer.backgroundColor = UIColor.clear.cgColor
return cell
}
Draw card - это простой переключатель состояния опроса, где вызывается хорошая функция для создания карты, подобной той, которую я выложил ранее: drawSurveyEnded