내 사용자 지정 셀에 타이머가 있습니다. 카운트 다운이 0에 도달하면 위임 메서드를 호출하고 셀이 자동으로 삭제됩니다. 문제는 두 번째 셀이 0에 도달하면 내 응용 프로그램이 오류 fatal error: Index out of range
과 충돌합니다. 내 사용자 지정 셀 I 설정에서치명적인 오류 : 두 번째 셀 삭제시 범위를 벗어나는 인덱스
내 데이터 : cellForRowAt 안에 내 ViewController
내가 설정 내 셀 데이터에서
protocol MyDelegateName {
func removeOfferExpired(offerId: String, indexPath: IndexPath)
}
class MyCustomCell: UITableViewCell {
var offer:Offers?
var cellIndexPath:IndexPath?
var delegate:MyDelegateName?
func setupData(offer:Offers, indexPath:IndexPath){
self.offer = offer
self.cellIndexPath = indexPath
//...other code not relevant
}
//When the time reach zero I call the following method
func updateTime() {
if timeLeft > 0 {
timeLeft = endTime.timeIntervalSinceNow
offerExpiresLabel.textColor = UIColor.white
offerExpiresLabel.text = timeLeft.hmmss
}else {
offerExpiresLabel.textColor = UIColor.red
offerExpiresLabel.text = "Offer Expired"
timer.invalidate()
self.delegate?.removeOfferExpired(offerId: (self.offer?.offer_id!)!, indexPath: self.cellIndexPath!)
}
}
: 다음
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let offer = offers[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! MyCustomCell
cell.setupData(offer: offer, indexPath: indexPath)
cell.delegate = self
return cell
}
는 func removeOfferExpired(offerId: String, indexPath: IndexPath)
안에 내가 사용하는 것을 시도했다 :
1. self.offers.remove(at: indexPath.row)
self.tableView.reloadData()
2. self.offers.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
self.tableView.reloadData()
3. //and even try to "wrap" it inside begin/end updates
tableView.beginUpdates()
self.offers.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
항상 두 번째 충돌합니다. 첫 번째 셀을 삭제 한 후 setupData
의 셀에 할당 한 indexPath가 같지 않지만 나머지 셀의 indexPath를 업데이트하는 방법은 reloadData
이라고 생각합니다.
셀이 다시로드되면 타이머를 잊지 않았습니까? 셀 외부에서 타이머/제거 로직을 이동해야합니다. 예를 들어, 모델 개체를 만들고 목록을 포함하고 컨트롤러로 제거되는 항목을 위임하는 등의 작업을 수행해야합니다. – vojer
@vojer 감사합니다. 그러나 충돌이 타이머와 관련이 없다고 생각하지 않습니다. – mat
이전 타이머가 실행되면 델리게이트가 더 이상 화면에없는 셀에서 전화를받을 수 있습니다. – vojer