2016-04-07 14 views
1

'dan sonra çöküyor Kullanıcı kaydırma yaparken, tablo görünümümün altına dinamik olarak satır eklemeye çalışıyorum. Buradaki fikir, kullanıcının bunu fark etmemesi ve "sonsuz" olarak kaymasıdır.Uygulama, insertRowsAtIndexPaths

Kullanıcı tablonun alt 40 hücresine ulaştığında, tabana 100 yeni hücre çizmek istiyorum.

Şimdi

 tableView.beginUpdates() 
     tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .None) 
     tableView.endUpdates() 

uygulama tableView.endUpdates() çökmesine görünüyor çağırdığınızda: Ben güncellemeyi gerçekleştirmeden önce

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 .. 9]'

şiddetle UITableView benim dataProvider doğru güncellenir inanıyoruz. Neden çöküyor bilmiyorum.

Bu amaçla çok basit bir uygulama yaptım. Herkes aşağıdakilerin neden çökeceğini anlıyor?

private let CellTreshold: Int = 100 
var cellCount: Int = 100 

@IBOutlet weak var tableView: UITableView! 

// UITableViewDataSource 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return cellCount 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier") as! UITableViewCell 
    cell.index = indexPath.row 

    return cell 
} 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 

    if indexPath.row == cellCount - 40 { 
     print("indexPath = \(indexPath.row); inserting \(CellTreshold) cells") 
     var indexPaths = [NSIndexPath]() 

     let beginIndex = cellCount 
     let endIndex = cellCount + CellTreshold 
     for i in beginIndex ..< endIndex { 
      indexPaths.append(NSIndexPath(forRow: i, inSection: 0)) 
     } 
     cellCount += CellTreshold 

     tableView.beginUpdates() 
     tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .None) 
     tableView.endUpdates() 
    } 
} 
+1

'beginIndex = cellCount izin ile deneyin - 1'? Belki sonuncusundan sonra ekliyorsunuz – zcui93

+0

Son hücrelerden sonra yeni hücrelerin yerleştirilmesi fikriydi. Sorun, sokmanın ilgili olduğu tek bir hücrenin modifikasyonu için kullanılmak üzere tasarlanan yönteme uygulanmış olmasıdır, bu noktada, tüm tablo görünümünün düzgün şekilde yapılandırılması ve yeni satırların yerleştirilmesi için hazırlanması garanti edilmemektedir. Bu nedenle, böyle bir işlem işe yarasa da işe yaraysa bile güvenli değildir. –

+0

Hata ayıklamaya çalışmanın ve değerin ne olduğunu anlamaya değebilirim [0 .. 9] ' – zcui93

cevap

0

Aynı sorun vardı - kod çöküyordu, çünkü kod willDisplayCell'deydi. Teşekkür @ A-yaşamak Ben, böyle bir şey iyi çalışıyor onun scrollViewDidScroll kodumu taşındıktan sonra açıklama

için:

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    let offsetY = scrollView.contentOffset.y 
    let contentHeght = scrollView.contentSize.height 
    let scrollViewHeight = scrollView.frame.size.height 

    if (contentHeght - offsetY) < 3*scrollViewHeight { 
     //we are less than 3 page screens from the bottom 

     let rowNumbers = self.dataModel.getVisibleCells() 
     let newRowNumbers = self.dataModel.getMoreCells() 
     let newCells = newRowNumbers - rowNumbers 

     if newCells > 0 { 
      debugPrint("ae.ui adding cells \(newCells)") 

      let rowInd = rowNumbers - 1 
      var indexPaths = [IndexPath]() 
      for i in 1...newCells { 
       indexPaths.append(IndexPath(row: rowInd+i, section: 0)) 
      } 

      self.beginUpdates() 
      self.insertRows(at: indexPaths, with: .none) 
      self.endUpdates() 
     } 
    } 
} 
İlgili konular