im doing tableview controller with images and i used this code
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
var program : Program
program = programy[indexPath.row]
cell.textLabel?.text = program.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
var imageName = UIImage(named: programy[indexPath.row])
cell.imageView?.image = imageName
and im getting error in
var imageName = UIImage(named: programy[indexPath.row])
Cannot invoke 'init' with an argument list of type (named $T5)
You already get the name of the image from your array in the code above, so you might want to use it for the UIImage inintalization, too:
var imageName = UIImage(named: program.name)
We can see that programy contains Program types. UIImage(named: String) expects a String so you need to give that Program.name or Program.category. Try this
var imageName = UIImage(named: program.name)
Related
I am trying to pass in the String "random text" to equal the property imageURL that is located inside of the custom Cell UserCell from CellForRowAtIndexPath. In didSet of the cell class, I can print "random text" just fine, however, for reasons unrelated to this question, I need to print "random text" inside of the lazy var. When I try to print it inside the lazy var, or even awakeFromNib, it gives me nil. I have an idea of why this is happening. I'm assuming the compiler runs certain code for a custom cell class before initializing any self properties. I'm wondering if there is a way to get around that.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! UserCell
cell.imageURL = "random text"
return cell
}
Inside of the custom cell (UserCell)
class UserCell: UITableViewCell {
var imageURL: String? {
didSet{
print(imageURL) //prints the imageURL
}
}
lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
print(imageURL) /////prints nil
return imageView
}()
I am new to swift I have an issue that is when scrolling cells in table view cells are shaking or shivering when scrolling the table view .I follow the below code can any one tell me the reason .I tested in IOS simulator not in real time device .my question is why it is shaking in simulator
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("transportCell") as! UITableViewCell
cell.textLabel?.text = transportItems[indexPath.row]
var imageName = UIImage(named: transportItems[indexPath.row])
cell.imageView?.image = imageName
return cell
}
I suggest you to perform the following changes:
instead of the lines:
var imageName = UIImage(named: transportItems[indexPath.row])
cell.imageView?.image = imageName
use the lines:
let qos = Int(QOS_CLASS_USER_INITIATED.rawValue)
dispatch_async(dispatch_get_global_queue(qos, 0)) { () -> Void in
if let imageName = UIImage(named: transportItems[indexPath.row])) {
dispatch_async(dispatch_get_main_queue()) {
self.cell.imageView?.image = imageName
}
}
}
In the tableView(tableView:, cellForRowAtIndexPath:) -> UITableViewCell method, how to set the optional imageView through the cell?
cell!.imageView?.image = someLoadedImage in this case, if the imageView: UIImageView? property of the cell is nil when constructed, then the assignment will be failed, right?
According to The "Swift Programming Guide", john.residence?.address = someAddress, "In this example, the attempt to set the address property of john.residence will fail, because john.residence is currently nil" (Optional Chaining Chapter).
class Residence {
...
var address: Address?
}
class Person {
var residence: Residence?
}
let john = Person()
let someAddress = Address()
john.residence?.address = someAddress // will fail
here is the code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier)
as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: simpleTableIdentifier)
}
let image = UIImage(named: "star")
let highlightedImage = UIImage(named: "star2")
cell!.imageView?.image = image // can compile and run
cell!.imageView?.highlightedImage = highlightedImage
cell?.textLabel!.text = dwarves[indexPath.row]
return cell!
}
We have reason to check if the cell is nil because if there is no available reusable cell for us in the queue to use at this time we have to create a new one.
I am trying to store an image in a variable, but my application is getting crashed on the below given line:
let theImage = UIImage(named: self.arrImages[indexPath.row])!
Here, arrImages is an array which contains images.
Crash which I am getting is
Fatal error: unexpectedly found nil while unwrapping an Optional values
Edit 1
As required by #Duncan C & #Valentin
Declaration:
var arrImages = [String]()
Using it in delegate of UICollectionView
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView == self.collectionView
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ImageCell
cell.imageView.image = nil
var theImage = UIImage()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
if let image = UIImage(named: self.arrImages[indexPath.row]) {
// Do whatever you like with the image here
theImage = UIImage(named: self.arrImages[indexPath.row])!
}
dispatch_async(dispatch_get_main_queue(), {
cell.imageView.image = theImage
cell.imageView.contentMode = .ScaleAspectFit
});
})
return cell
}
You are not providing enough information, and some of the things you did say do not make sense.
Post the declaration of your arrImages, as well as the code that installs values into the array.
You say that your array contains images, but your code is expecting the array to contain image filenames (strings).
Next, you need to break your code into steps and debug it:
let index = indexPath.row
let imageName = self.arrImages[index];
println("At index \(index), imageName = \"\(imageName)\"")
let image = UIImage(named: imageName)
println("Image = \(image)")
Try binding it with
if let image = UIImage(named: ...) {
// Do whatever you like with the image here
}
I am developing a simple table app where I want to display Image in every cell and I have done this and one Image is displayed In all cells :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
// Create the cell
var cell : UITableViewCell = self.TableViewData.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.textLabel?.text = self.iteams[indexPath.row]
cell.imageView?.image = UIImage(named: "creme_brelee.jpg")
return cell
}
It works fine but I want to display more Images for different cell and for that I have taken on array:
var thumbils : [String] = ["egg_benedict.jpg", "mushroom_risotto.jpg", "full_breakfast.jpg", "hamburger.jpg", "ham_and_egg_sandwich.jpg", "creme_brelee.jpg", "white_chocolate_donut.jpg", "starbucks_coffee.jpg", "vegetable_curry.jpg", "instant_noodle_with_egg.jpg", "noodle_with_bbq_pork.jpg", "japanese_noodle_with_pork.jpg", "green_tea.jpg", "thai_shrimp_cake.jpg", "angry_birds_cake.jpg", "ham_and_cheese_panini.jpg"]
but now I dont know how to display different image for different index in tableView.
I know In objective-C we can do like this:
cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
But I don't know how to do this in swift.
Please help me for this.
You can use below code to fetch an image from array:
cell.imageView?.image = UIImage(named: thumbils[indexPath.row])
use below code
cell.imageView?.image = UIImage(self.iteams(indexPath.row)
var imagesArray = [UIImage]()
imagesArray.append(UIImage(named: "mypic.png")!)
cell.imageView?.image = imagesArray[indexPath.row]
return cell