Haneke set Button Image in UITableView - uiimageview

I got a button named "avatarImage" that should display the user's avatar
tried this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//do other cell stuff....
//does not present anything, but actually gets the image!
cell.avatarImage.imageView!.contentMode = .ScaleAspectFill
cell.avatarImage.imageView!.frame = cell.avatarImage.frame
cell.avatarImage.imageView!.hnk_setImageFromURL(imgURL!)
//let foo = UIImageView(frame:cell.frame)
//self.view.addSubview(foo)
//foo.hnk_setImageFromURL(imgURL!)
//tried this for testing, works perfectly fine
}
also when adding a 'UIImageView' to the cell, it will be set correctly. Is there any issue that you can not target button.imageview? directly?
Any hints?

Related

Dynamic Type does not work with tableView.dequeueReusableCell(withIdentifier:)

I'm experiencing a quite strange behaviour for my UITableViewCells with style .subtitle regarding Dynamic Type. When using
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DesignElementCell", for: indexPath) //Dynamic Type does not work properly in this case
cell.textLabel?.text = "Test"
cell.detailTextLabel?.text = "Test"
return cell
}
the output looks like
As you can see the header text is scaled fine, while cell labels are not. When using
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "DesignElementCell")
cell.textLabel?.text = "Test"
cell.detailTextLabel?.text = "Test"
return cell
}
Everything works fine:
The strangest thing in the first case is, that when I change dynamic type size in settings and head back to my app, suddenly everything seems to work as long as I'm not popping my ViewController. Does anyone experienced something similar? Have a nice day :)
First, check if you registered cell, and set delegate&dataSource of tableView in ViewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(Cell.self, forCellReuseIdentifier:"CellIdentifier")
Then, define rowHeight & estimatedRowHeight of tableView in ViewDidLoad()
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 150

I have a bug with a Swift table view cell action

I'm using editActionsForRowAtIndexPath with Swift to add a option to my app cells.
My app is a homework list app, where you can set its subject, its category, the date you need it ready for and add a description. Also, there is a option that when you swipe the cell to the left you will be able to set the homework as Done. To show that, I created a label and added it to the cell, that if the homework is pending, the label will be Pending. If it was set as done, it will be Concluded. The thing is that when I'm setting ONE cell as done, it is setting all the other cells, which is not what I want.
The code I'm using to say that I only want THIS cell to be set as done is this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("AtivCell") as? AtivCell {
if !isPending {
pendencia[indexPath.row] = false
saveData()
}
cell.configureCell(materias[indexPath.row], data: datas[indexPath.row], descricao: descricoes[indexPath.row], pendencia: pendencia[indexPath.row], categoria: categorias[indexPath.row])
return cell
} else {
return AtivCell()
}
}
Here is the entire project, I really need help, I don't think I did anything wrong.
I've tried deleting the app from my iPhone (where I'm testing), tried closing Xcode, tried everything, it just does not work!!!
Link: https://github.com/HenriqueDoura/Agenda/tree/master/agenda-app
You should be using dequeueReusableCellWithIdentifier with the forIndexpath parameter, which always returns a cell. The one you've used requires you to test if it's nil and create one, which you are doing in the else clause and then not initialising it.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("AtivCell", forIndexPath: indexPath) as? AtivCell {
cell.configureCell(materias[indexPath.row], data: datas[indexPath.row], descricao: descricoes[indexPath.row], pendencia: pendencia[indexPath.row], categoria: categorias[indexPath.row])
return cell
}
You can use your current function if you want, but you need to declare cell as var, and create it if dequeue returns nil, and then set its values.
Also your !isPending code will not work properly. Within editActionsForRowAtIndexPath, you should be setting pendencia[indexPath.row] = false.

Table View Cell Image resizes when swipe to edit

I have Constraints set for the image view in the background but for some reason when I swipe to get the Delete the image gets resized.
First Try to changing your imageView content mode to .AspectFit. The second image is actually correct if you want the whole image to show. It looks like it's redrawing image when it starts becoming interactive. Try calling cell.yourImageView.layoutIfNeeded() when loading the cell to fix your problem. If this is a UIViewController and that does not work try moving the code for autodiminsions to the actual functions.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return closeGuess //maybe 200
}

Saving reorder of TableView cells using NSCoding

I have an iOS project I'm working on using Xcode7 and Swift2. I have a TableView that fetches an array from NSCoding. I have it started so the user can reorder the TableViewCells in the TableView. However I need it to save the new order once the user is finished and clicks 'done' which is a UIBarButtonItem. I have a value in my NSCoding object called cellOrder. I looked here and saw this for CoreData. How how would I do this for NSCoding and where do I save it?
I have the following code started for the TableViewCell movement:
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true}
func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
let itemToMove = details[fromIndexPath.row]
details.removeAtIndex(fromIndexPath.row)
details.insert(itemToMove, atIndex: toIndexPath.row)
}
details is the array my data is kept in, using NSCoding.
I decided the best way for me was to save the details array to NSUserDefaults after the the reorder was done. I saw here how to convert the NSObject to NSData to be saved. My code for the moveRowAtIndexPath section is now:
func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
let itemToMove = details[fromIndexPath.row]
details.removeAtIndex(fromIndexPath.row)
details.insert(itemToMove, atIndex: toIndexPath.row)
// Archive NSObject and save as NSData
let myData = NSKeyedArchiver.archivedDataWithRootObject(details)
// NSUserDefaults Save for Reorder of cells
reportDefaults.setObject(myData, forKey: "savedReportListKEY")
reportDefaults.synchronize()
// NSCoding Save
saveReport()
}

Passing an image when taped on uicollectionview to a detail view controller in swift

Ive been at this for a while but cant seem to crack it in swift
I want a user to be able to select an image in uicollectionView and for that image to appear in a detailed view controller, i can do this quite easily with a peice of text,
and i can do this when there is a static array of images preloaded. but i cant seem to get anywhere with a collectionview which is loaded with images from a camera.
I understand i need to use
override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
}
and this function to isolated selected cell.
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
}
I do have these outlets
#IBOutlet weak var collectionView: UICollectionView!
var images = [UIImage]()
image picker stores all images to this array by
images.insert(newImage, atIndex: 0)
when the array would be passed to the detailviewcontroller, i understand that would have to be copied into another local array and then how would i get the current image that was highlighted to be shown first, perhaps using indexPath.Row
Regards
I'm not using segues, and actually I don't quite understand what your problem is, but I'll try to show you how it could be achieved.
First of all, you have an array with images, so I believe your image should be accessed as images[indexPath.row]
let's suppose that you already have UIViewController class with UIImageView in it.
if so, you can write something like that:
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let myVC = MyViewController()
myVC.imageView.image = images[indexPath.row]
self.presentViewController(myVC, animated: true, completion: nil)
}
for modal or
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let myVC = MyViewController()
myVC.imageView.image = images[indexPath.row]
self.navigationController?.pushViewController(myVC, animated: true)
}
if you want to show it as navigational.
I believe that with segues it's basically the same but I think you have to use func prepareForSegue for, you know, preparing the segue (func performSegueWithIdentifier will just perform it). In prepareForSegue you should check identifier of your segue
if segue.identifier == "myIdentifier" {
//your code here
}
and if this identifier is right, put your commands to your myVC there.

Resources