2016-03-30 15 views
0

Ich habe einen UITableViewController mit UITableView (natürlich) und meine Frage ist, wie Daten in TableView in Runtime zu aktualisieren?tableView reloadData cellForRowAtIndexPath nicht aufgerufen

Da, wenn ich reloadData in viewWillAppear-Methode aufrufen, funktioniert es perfekt, aber später im Code tut es nichts.

Anzahl der Zeilen Methode wird aufgerufen und gibt Nicht-Null-Nummer, aber cellForRowAtIndexPath wird nicht aufgerufen, so wird Tabelle nicht aktualisiert ...

class CropTableViewController: UITableViewController { 

var photos = [Photo]() 

var photoAssets = [PHAsset]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.automaticallyAdjustsScrollViewInsets = false 
} 

override func viewWillAppear(animated: Bool) { 
    refreshData() 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print("rows called: \(photos.count)") // prints non-zero 
    return photos.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    print("cell called") 

    let cellIdentifier = "Cell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CropTableViewCell 

    let photo = photos[indexPath.row] 


    cell.photoImageView.image = photo.photo 

    return cell 
} 

func refreshData(){ 
    if (loadPhotos() != nil){ 
     photos = loadPhotos()! 
     tableView.reloadData() 
    } 

    print("refreshData called \(photos.count)") 
} 
} 

EDIT:

loadPhotos gerade ist, um Bilder von iphone laden Speicher:

func loadPhotos() -> [Photo]? { 
    return NSKeyedUnarchiver.unarchiveObjectWithFile(Photo.ArchiveURL.path!) as? [Photo] 
} 

Refresh von einer anderen Klasse in diesem Zyklus-Aufruf:

for asset in photoAssets { 
     let manager: PHImageManager = PHImageManager.defaultManager() 
     let scale: CGFloat = UIScreen.mainScreen().scale 
     let targetSize: CGSize = CGSizeMake(300.0 * scale, 180.0 * scale) 

     manager.requestImageForAsset(asset, targetSize: targetSize, contentMode: .AspectFill, options: self.requestOptions, resultHandler: {(image, info) -> Void in 
      let photo = Photo(photo: image!) 
      self.photos.append(photo!) 
      print("photos count \(self.photos.count)") 
      self.savePhotos() 
      CropTableViewController().refreshData() 
     }) 
    } 
+0

http://stackoverflow.com/questions/7712325/cellforrowatindexpath-not-called – hasan83

+0

Was bedeutet 'loadPhotos'do? Ist es asynchron? Ihr Code in 'refreshData' sollte den Wert von' loadPhotos() 'erhalten und das für non-nil überprüfen, anstatt die Funktion zweimal aufzurufen. Können Sie zeigen, wie Sie 'refreshData' später im Code aufrufen? – Paulw11

+0

Ich habe die Frage bearbeitet. –

Antwort

0

Erschossen im Dunkeln. Versuchen Sie folgendes:

func refreshData(){ 
    if let photos = loadPhotos() { 
     dispatch_async(dispatch_get_main_queue()) 
     { 
      photos = photos 
      tableView.reloadData() 
     } 
    } 

    print("refreshData called \(photos.count)") 
} 
Verwandte Themen