简体   繁体   中英

tableView reloadData cellForRowAtIndexPath not called

I have an UITableViewController with UITableView (of course) and my question is how to update data in tableView in runtime?

Because when I call reloadData in viewWillAppear method, it works perfectly, but later in code it does nothing.

Number of rows method is called and returns non-zero number, but cellForRowAtIndexPath is not called so table is not updated...

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 is just loading images from iphone memory:

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

refreshData is called from another class in this cycle:

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()
        })
    }

Shot in the dark. Try this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM