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
Related
I have a table view that has 100 rows. What I'd like to do is set an image for the row if the index of the row is in my array that I have. What I've found is that it only sets the image for the first number in the array. So, in this example, I have [2,3,4,5] in my array. The result is that it is setting the image for the row where index = 2. Rather than for all the numbers. Any help on this would be greatly appreciated!
var completedWorkoutsArray = [2,3,4,5]
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")! //1.
let text = data[indexPath.row]
cell.textLabel?.text = text
for i in completedWorkoutsArray {
if(indexPath.row == i) {
let image: UIImage = checkmark!
cell.imageView!.image = image
}
else
{
let image: UIImage = redX!
cell.imageView!.image = image
}
}
return cell
}
You want something like this:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")!
let text = data[indexPath.row]
cell.textLabel?.text = text
let image = completedWorkoutsArray.contains(indexPath.row) ? checkmark : redX
cell.imageView.image = image
return cell
}
Your code fails because it loops through all values in the array. It sets the image each and every time. The last image set is the one you see.
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
}
}
}
I have the following problem.
I have set up a search bar in a tableView written in swift. Each tableView cell has a label and an image. The problem is that when I try to search the searchDisplayController displays the right label but the wrong image (it displays the first image from the array).
when the app loads:
when I try to search:
As you can see the images start over from the beginning and the image view doesn't display the correct image for the iPhone cell.
Thanks in advance for your answers.
EDIT1: cellForRowAtIndexPath code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell
var friend : FriendItem
if (tableView == self.searchDisplayController?.searchResultsTableView)
{
friend = self.filteredFriends[indexPath.row]
}
else
{
friend = self.friendsArray[indexPath.row]
}
cell.label1.text = friend.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
let item = friendsArray[indexPath.row]
cell.setCell(item.name, image1: item.image)
return cell
}
You are taking item from friendsArray, but you should take if from filteredFriends array when you are searching. Try cell.setCell(friend.name, image1: friend.image)
I'm trying to create an RSS app for practice. So my tableView cell has both textLabel and image. My textLabel's text and image data come from a dictionary stored in Core Data. Some cells have images, and some don't. The initial load of tableView looks fine to me. But when I scroll down and up, cell's images seem to change. The cells' textLabel text doesn't change though.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
self.configureCell(cell, atIndexPath: indexPath)
return cell
}
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.textLabel!.text = object.valueForKey("title")!.description
var image: UIImage?
if let imageData = object.valueForKey("imageData") as? NSData {
image = UIImage(data: imageData)
let itemSize = CGSizeMake(80, 80)
UIGraphicsBeginImageContext(itemSize)
let imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height)
image?.drawInRect(imageRect)
cell.imageView?.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
If the initial tableView loads correctly, that means my Core Data stores my data's mapping correctly. But when scrolling down and up, configureCell is being called again since it needs to redraw the cell. cell.textLabel!.text = object.valueForKey("title")!.description is set correctly again, but not image here. Don't know why it behaves like this. Please give some pointer.
I had this same problem once and I think it has something to do with the reused cell not being in the default state so sometimes the image you set is being reused. To fix it, I just did an else condition on imageData and set the image to a default image if no image was found. I'm sure you could set it to nil here as well.
if let image = UIImage(contentsOfFile: imagePath) {
cell.imageView.image = image
} else {
// Default image or nil
cell.imageView.image = UIImage(named: "Calendar")
}
And I wouldn't suggest storing images as raw data in core data, as this can be very inefficient. Instead, download them to your documents directory and store a file name in core data.
What i did was remove the image from imageView and set image again. It works for me.
cell.imageView.image = nil
cell.imageView.image = UIImage(named:"Calendar")
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)