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)")
}
Related
I put UICollectionView inside table cell in Home page. Each category row has their contents image. But, my problem is that when I run the app, the images under second row/second category do not show whenever I run the app.
The images will show after I click more button and it will goes to details UI. Then, I click on Back button and reach the Home page. At this time, the images shows properly. I tried many codes but I don't know where it is wrong.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tableView.reloadData()
self.tableView.reloadData()
self.tableView.layoutIfNeeded()
}
override func viewDidAppear(_ animated: Bool) {
tableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//getSearchWords()
DispatchQueue.main.async {
self.tableView.reloadData()
}
self.tableView.reloadData()
self.tableView.layoutIfNeeded()
}
override func viewDidLoad() {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// debugPrint("HOme Table View Cell")
let cell = tableView.dequeueReusableCell(withIdentifier: "homecell") as! HomeCategoryRowCell
let list = sections[(indexPath as NSIndexPath).row]
cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
cell.collectionViewOffset = self.storedOffsets[indexPath.row] ?? 0
DispatchQueue.main.async {
cell.categoryTitle.text = list.package_name
cell.mainAssociatedURL.text = list.package_url
}
cell.categoryTitle.font = UIFont.boldSystemFont(ofSize: 17.0)
cell.collectionView.tag = indexPath.row
cell.parentVC = self
cell.collectionView.reloadData()
return cell
}
extension Home : UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return sections[collectionView.tag].packageTable.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! HomeVideoCell
let list = sections[collectionView.tag].packageTable[indexPath.row]
if(list.poster_image_url != StringResource().posterURL){
let url = NSURL(string: list.poster_image_url)
do{
if let url = url {
_ = try Data(contentsOf:url as URL)
poster_url = list.poster_image_url
}
}catch let error {
//debugPrint("ERRor ::\(error)")
poster_url = StringResource().posterURL
}
}else{
poster_url = StringResource().posterURL
}
AsyncImageLoader.sharedLoader.imageForUrl(urlString: poster_url) { (image, url) -> () in
DispatchQueue.main.async(){
cell.movieTitle.text = list.name
if(url == StringResource().posterURL){
cell.imageView.image = UIImage(named: "sample_cover")
}else{
cell.imageView.image = image
}
}
}
// cell.layer.shouldRasterize = true
// cell.layer.rasterizationScale = UIScreen.main.scale
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Home Collection view at row \(collectionView.tag) selected index path \(indexPath)")
let list = sections[collectionView.tag].packageTable[indexPath.row]
self.prefs.set(list.poster_image_url, forKey: "poster_image_url")
self.prefs.set(list.name, forKey: "name")
self.prefs.set(list.assets_id, forKey: "VEDIO_ID")
debugPrint(list.name)
debugPrint(list.assets_id)
}
}
These above codes are written in Home.swift.
In HomeCell.swift,
class HomeCell : UITableViewCell {
var parentVC: UIViewController?
#IBOutlet weak var collectionView: UICollectionView!
#IBOutlet weak var categoryTitle: UILabel!
#IBOutlet weak var SeeAll: UIButton!
#IBOutlet weak var mainAssociatedURL: UILabel!
let prefs:UserDefaults = UserDefaults.standard
var catId: String! = ""
var associated_url: String!
override func awakeFromNib() {
super.awakeFromNib()
SeeAll.setTitle(NSLocalizedString("See All >", comment: ""), for: .normal)
SeeAll.titleLabel!.font = UIFont (name: "Tharlon", size: 14)
}
#IBAction func btnSeeAll(_ sender: Any) {
catId = categoryTitle.text!
associated_url = mainAssociatedURL.text!
self.prefs.setValue(1, forKey: "PROVIDER_ID")
self.prefs.set(catId, forKey: "PROVIDER_NAME")
self.prefs.set(associated_url, forKey: "associated_url")
}
override func layoutSubviews() {
super.layoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}
}
extension HomeCategoryRowCell {
func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) {
self.collectionView.delegate = dataSourceDelegate
self.collectionView.dataSource = dataSourceDelegate
self.collectionView.tag = row
self.collectionView.setContentOffset(self.collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
self.collectionView.reloadData()
}
var collectionViewOffset: CGFloat {
get { return collectionView.contentOffset.x }
set { collectionView.contentOffset.x = newValue }
}
}
Can anyone help me please?
Edit controller like below, you are calling too many reloadData.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.tableView.layoutIfNeeded()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//getSearchWords()
DispatchQueue.main.async {
self.tableView.reloadData()
}
self.tableView.layoutIfNeeded()
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
and add:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView .deselectRow(at: indexPath, animated: false)
}
I'm using UIImageView+AFNetworking to load images in table cell, but
images downloaded from server aren't shown in cell until scroll down and up again. I thought that I needed to reload data in tableView, but I don't know where to put said reload code.
Here's the code:
#IBOutlet weak var refresherTool: UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
let _ = dataRefresher.sharedInstance.refreshVideos(fromUrl: "https://api.vid.me/videos/featured", withLimit: "100", withOffset: "0", completion: {
featuredVideos in dataRefresher.sharedInstance.featuretVideos = featuredVideos
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
}
#IBAction func refreshBtn(_ sender: UIRefreshControl) {
let _ = dataRefresher.sharedInstance.refreshVideos(fromUrl: "https://api.vid.me/videos/featured", withLimit: "100", withOffset: "0", completion: {
featuredVideos in dataRefresher.sharedInstance.featuretVideos = featuredVideos
self.refresherTool.endRefreshing()
DispatchQueue.main.async{
self.tableView.reloadData()
}
})
refresherTool.endRefreshing()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if dataRefresher.sharedInstance.featuretVideos == nil {
return 0
}
return 100
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "featuredCell", for: indexPath)
// if dataRefresher.sharedInstance.featuretVideos == nil { return cell}
let newImage = UIImageView()
newImage.setImageWithUrl(URL.init(string: dataRefresher.sharedInstance.getVideosThumbnailUrls(fromVideos: dataRefresher.sharedInstance.featuretVideos)[indexPath.row])!, placeHolderImage: #imageLiteral(resourceName: "first"))
// newImage.setImageWithUrl(URL.init(string: urls[indexPath.row])!)
cell.imageView?.image = newImage.image
cell.textLabel?.text = dataRefresher.sharedInstance.getVideosTitels(fromVideos: dataRefresher.sharedInstance.featuretVideos)[indexPath.row]
print(indexPath.row)
print(newImage.image!)
return cell
}
The setImageWithUrl method is asynchronous, meaning that if the referenced image is not in the local cache yet, it will only be downloaded some time in the future.
However, your code tries to get the image out right away:
let newImage = UIImageView()
newImage.setImageWithUrl(..., placeHolderImage: ...)
cell.imageView?.image = newImage.image <-- synch call, image is nil here at first!
Fortunately, there's no need to reload the table view or do any other arcane tricks; just stop doing unnecessary things.
Remove the temporary UIImageView and load the image into the cell's UIImageView directly.
The above code becomes:
cell.imageView?.setImageWithUrl(..., placeHolderImage: ...)
I have a situation where I am filling out a form and set image from library then storing in a directory.
I am able to get image from the directory but tableview list flickering in the middle of scroll, so I am working with method of dispatch_async but not able to do.
If anybody has a solution, please let me know.
Here is my code.
import UIKit
func getDocumentsURL() -> NSURL {
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
return documentsURL
}
func fileInDocumentsDirectory(filename: String) -> String {
let fileURL = getDocumentsURL().URLByAppendingPathComponent(filename)
return fileURL.path!
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var marrEmpData : NSMutableArray!
#IBOutlet var MyTable: UITableView!
#IBAction func addEmpInfo(){
}
override func viewWillAppear(animated: Bool) {
self.getStudentData()
}
func getStudentData()
{
marrEmpData = NSMutableArray()
marrEmpData = ModelManager.getInstance().getAllStudentData()
MyTable.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
MyTable.delegate = self
MyTable.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
//return names.count;
return marrEmpData.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:CustomCell = self.MyTable.dequeueReusableCellWithIdentifier("cells") as! CustomCell
let emp:EmpInfo = marrEmpData.objectAtIndex(indexPath.row) as! EmpInfo
let randomNum = "Image-\(indexPath.row+1).png"
let imagePathOne = fileInDocumentsDirectory(randomNum)
dispatch_async(dispatch_get_main_queue()) {
if let loadedImage = self.loadImageFromPath(imagePathOne) {
print("view Loaded Image: \(loadedImage)")
cell.photo.image = loadedImage
}
else {
cell.photo.image = UIImage(named: "default_user")
}
}
// user profile
cell.name.text = emp.full_name
cell.age.text = emp.age
cell.phone.text = emp.phone
return cell
}
// which one is tapped
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You tapped cell number \(indexPath.row).")
}
// load image of user
func loadImageFromPath(path: String) -> UIImage? {
print("image-----\(path)")
let image = UIImage(contentsOfFile: path)
if image == nil {
print("missing image at: \(path)")
}
print("Loading image from path: \(path)") // this is just for you to see the path in case you want to go to the directory, using Finder.
return image
}
}
You are using dispatch_async but you are dispatching to the main queue which is the thread you are already on. You should dispatch to a background thread, load the image, then dispatch to the main thread and update the UI.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0))
{
let image = self.loadImageFromPath(imagePathOne)
dispatch_async(dispatch_get_main_queue())
{
if let loadedImage = image
{
print("view Loaded Image: \(loadedImage)")
cell.photo.image = loadedImage
}
else
{
cell.photo.image = UIImage(named: "default_user")
}
}
}
I have a UICollectionView to display photos from a device's album with the Photos framework. The photos are correctly displayed, but if I scroll fast (like when you tape at the top of the screen to go to the top of the collectionView), I have some photos which are not at the good indexPath. I just need to scroll a bit to put the bad photo out of the screen, and everything go back in place.
I clean the cell during prepareForReuse by canceling the current request.
I presume it's a problem with the asynchronous request of PHImageManager, but I don't know how to avoid this problem.
Here some code :
View Controller
extension AlbumDetailViewController : UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photoList.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PhotoCell", forIndexPath: indexPath) as! PhotoCollectionCell
cell.setImage(photoList.objectAtIndex(indexPath.row) as! PHAsset)
return cell
}
}
Custom CollectionViewCell
class PhotoCollectionCell: UICollectionViewCell {
#IBOutlet weak var imageView: UIImageView!
var requestId: PHImageRequestID!
let manager = PHImageManager.defaultManager()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
self.imageView.image = nil
manager.cancelImageRequest(self.requestId)
}
func setImage(asset: PHAsset) {
let option = PHImageRequestOptions()
option.resizeMode = .Fast
option.deliveryMode = .HighQualityFormat
self.requestId = manager.requestImageForAsset(asset, targetSize: CGSize(width: self.frame.size.width * UIScreen.mainScreen().scale, height: self.frame.size.height * UIScreen.mainScreen().scale), contentMode: PHImageContentMode.Default, options: option, resultHandler: {(result, info)->Void in
self.imageView.image = result
})
}
}
Thank you
Before calling requestImageForAsset method, set cell.tag equal indexPath.item, and after when getting image in callback, check indexPath.item and cell.tag, if this is matched do that
self.imageView.image = result. For example
cell.tag = indexPath.item
if let asset = assets?[indexPath.item] {
let option = PHImageRequestOptions()
option.isSynchronous = false
option.resizeMode = .exact
option.isNetworkAccessAllowed = true
DispatchQueue.global(qos: .userInitiated).async {
manager.requestImage(for: asset, targetSize: Constant.targetSize, contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
DispatchQueue.main.async {
if cell.tag == indexPath.item {
cell.imageView.image = result
}
}
})
}
}
Hope, it helps you
I have added a table view, and I am display image in the cells. I have also added this code:
tableView.estimatedRowHeight = 175
tableView.rowHeight = UITableViewAutomaticDimension
So that the cells resize depending on the image.
When I launch my app though, I get this :
And the images do not load untill I start scrolling...If I scroll down half the page then go back to the top, I get this(which is correct):
Also if I remove the like button and just has the image alone in a cell, when I launch my app if I wait 3 seconds without touching anything the cells resize on they're own..?!
Any ideas? I have researched on google and tried the odd solution for the older versions of Xcode, But nothing seems to work!
Here is the rest of my code from the TableViewController:
extension TimelineViewController: UITableViewDataSource {
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 46
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return timelineComponent.content.count
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("PostHeader") as! PostHeaderTableViewCell
let post = self.timelineComponent.content[section]
headerCell.post = post
return headerCell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as! PostTableViewCell
//cell.postImageView?.image = UIImage(named: "Background.png")
let post = timelineComponent.content[indexPath.section]
post.downloadImage()
post.fetchLikes()
cell.post = post
cell.layoutIfNeeded()
return cell
}
}
extension TimelineViewController: UITableViewDelegate {
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
timelineComponent.targetWillDisplayEntry(indexPath.section)
}
Download image code:
func downloadImage() {
// 1
image.value = Post.imageCache[self.imageFile!.name]
if image is not downloaded yet, get it
if (image.value == nil) {
imageFile?.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
if let data = data {
let image = UIImage(data: data, scale: 2.0)!
self.image.value = image
// 2
Post.imageCache[self.imageFile!.name] = image
}
}
}
}
// MARK: PFSubclassing
extension Post: PFSubclassing {
static func parseClassName() -> String {
return "Post"
}
override class func initialize() {
var onceToken : dispatch_once_t = 0;
dispatch_once(&onceToken) {
// inform Parse about this subclass
self.registerSubclass()
// 1
Post.imageCache = NSCacheSwift<String, UIImage>()
}
}
}
And here is my TableViewCell:
var post: Post? {
didSet {
postDisposable?.dispose()
likeDisposable?.dispose()
if let oldValue = oldValue where oldValue != post {
oldValue.image.value = nil
}
if let post = post {
postDisposable = post.image
.bindTo(postImageView.bnd_image)
likeDisposable = post.likes
.observe { (value: [PFUser]?) -> () in
if let value = value {
//self.likesLabel.text = self.stringFromUserList(value)
self.likeButton.selected = value.contains(PFUser.currentUser()!)
// self.likesIconImageView.hidden = (value.count == 0)
} else {
//self.likesLabel.text = ""
self.likeButton.selected = false
//self.likesIconImageView.hidden = true
}
}
}
}
}
Any help is really appreciated!
I guess, you need to reload the cell when the image is finally loaded, because tableView needs to recalculate cell height (and the whole contentHeight) when image with new size arrives
post.downloadImage { _ in
if tableView.indexPathForCell(cell) == indexPath {
tableView.reloadRowsAtIndexPaths([indexPath], animation: .None)
}
}
and downloadImage method needs to call completion closure. Something like that.
func downloadImage(completion: ((UIImage?) -> Void)?) {
if let imageValue = Post.imageCache[self.imageFile!.name] {
image.value = imageValue
completion?(imageValue)
return
}
//if image is not downloaded yet, get it
imageFile?.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
if let data = data {
let image = UIImage(data: data, scale: 2.0)!
self.image.value = image
// 2
Post.imageCache[self.imageFile!.name] = image
completion?(image)
} else {
completion?(nil)
}
}
}