Here I am displaying images in imageView. I am able to display image but only single image in all rows of TableView I am using SDWebImage with swift for the first time. So I want to pass the array in the following code to display the respective images in each row.Here I am successfully displaying name by passing it through array now just wanted to display the images.
NOTE : see the code in comments below but its giving error want to pass the same array but don't know how to write in swift.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
let strTitle : NSString = arrDict[indexPath.row].valueForKey("clip_name") as! NSString
cell.clipName.text = strTitle as String
cell.videoImage.sd_setImageWithURL(NSURL(string:"http://example.com/test/images/clips", arrDict(indexPath.row).objectForKey("clip_image_path")))
return cell
}
You need to append your image name with your URL like this way.
let imagePath = String(format: "http://example.com/test/images/clips/%#",arrDict[indexPath.row].valueForKey("clip_image_path") as! NSString)
cell.videoImage.sd_setImageWithURL(NSURL(string: imagePath))
Related
I developing news app. I using RSS feed with XML Parsing. I want RSS feed add images. I have defined the necessary function for images. But I do not know how to use a variable when I get the photo in RSS. My application is currently receiving the photo from the URL. All photos are the same. How can I do that? Sorry for my bad English.
//UItableView Data source
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return haberler.count
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell:EmojiTableViewCell = tableView.dequeueReusableCell(withIdentifier: "myCell")! as UITableViewCell as! EmojiTableViewCell
if (cell.isEqual(NSNull.self))
{
cell = Bundle.main.loadNibNamed("myCell", owner: self, options: nil)?[0] as! UITableViewCell as! EmojiTableViewCell
}
let emojiTitle = (haberler.object(at: indexPath.row) as AnyObject).value(forKey: "title") as! NSString as String
cell.emojiTitleLabel.text = emojiTitle.trimmingCharacters(in: .whitespaces)
cell.emojiImageView.downloadFrom(link: "http://teknolojix.net/wp-content/uploads/2014/10/Below-is-a-rendering-of-the-page-up-to-the-first-error-hatası.jpg", contentMode: UIViewContentMode.scaleAspectFill)
return cell
}
Your problem is very simple. you have pass the same link on the "downloadFrom" function. Please use "haberler" array to get the different image photo url. For example I used you above mentioned code to give you proper understanding.
CODE:
cell.emojiImageView.downloadFrom(link:haberler[indexPath.row].YOUR_PHOTO_URL_KEY_NAME, contentMode: UIViewContentMode.scaleAspectFill)
If you are not understand, please provide me "haberler" array so that I can give you proper solution with code.
Actually you are using hard-coded 'URL' to download image every time. You need to show image for every cell by getting different 'URL' from your data source same like you are getting 'title' for every cell. For example:
let emojiImageURL = (haberler.object(at: indexPath.row) as AnyObject).value(forKey: "url") as! NSString as String
cell.emojiImageView.downloadFrom(link: emojiImageURL, contentMode: UIViewContentMode.scaleAspectFill)
I have a collection view with custom cells. I want to populate cells with images by fetching data from two places. One is default image array and another is array which is fetched after parsing xml web services. So the final array count is summation of default array count and array count of web services.
How to fetch the images in cellForItemAt method of collection view?
can Anyone help me out in this regard?
From above Question
let cell:SubCategoryCollectionViewCell = self.collectionview3.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! SubCategoryCollectionViewCell
if defaultAnimalArray.count-1 >= indexPath.row{ // ex: 0...7
let item = defaultAnimalArray[indexPath.row]
cell.subThumbImg?.image = UIImage(named: item as! String)
}else{
//If now defaultAnimalArray.count = 8, indexPath = 8 , But array = 0...4, then,
let item = arrayFromAPI[indexPath.row-defaultAnimalArray.count] // 8-8 = 0
cell.subThumbImg?.image = UIImage(named: item as! String)
}
return cell
Alternative 1:-
Declare defaultArray as var.
Before return defautlArray.count+arrayFromAPI.count merge image array also . i.e., defaultArray = defaultArray+arrayFromAPI and return variable becomes return defaultArray.count as well as no changecellForItemAtIndexPath.
Alternative 2:
Declare defaultArray as var and Don't create arrayFromAPI. Just append new images in defaultArray in XML Parsing. After appending a new image reload UICollectionView and also return variable becomes return defaultArray.count
Alternative 3:
With the same code , the item on cellForItemAtIndexPath becomes,
let item = defaultAnimalArray+arrayFromAPI[indexPath.row]
set
collectionView.delegate = nil
collectionView.dataSource = nil
Until you get all your images from xml web services. When you get all your data then update your dataSource array like you said
count = default array count + array count from web services
and then set delegate and dataSource of collectionView. Then in
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifierCell", for: indexPath) as! UICollectionViewCell
/// Show Image Code here
let url : String = dataSource[indexPath.row]
if let _image = UIImage(named: url){
// This If Will Load the image if it save locally
} else {
// Image does not save locally, download image from URL
}
return cell
}
I have update my answer please look and tell me if you found anything difficult.
One thing you can use SDWebImage third party to download image from URL.
I'm writing a Swift app that displays photos fetched from server on each cell of my UITableViewController.
So far my code looks as follows:
func tableView(detailsPanel: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = detailsPanel.dequeueReusableCellWithIdentifier("cell") as! DetailsCell
let test:SingleTest = self.items[indexPath.row] as! SingleTest
if(test.photo != "") {
cell.myPhoto.af_setImageWithURL(NSURL(string: test.photo)!)
}
}
Now, the problem is that not every cell has a photo stored in cell.photo. Some of them are empty strings (""). In that situation when I quickly scroll through the table view, I see that those empty UIImageViews are filled with photos from other cells.
The quick fix for that seems to be adding an else block:
if(test.photo != "") {
cell.myPhoto.af_setImageWithURL(NSURL(string: test.photo)!)
} //this one below:
else {
cell.myPhoto.image = UIImage(named: "placeholderImg")
}
Now whenever there is no photo, the placeHolderImg will be displayed there. But... is there a way of avoiding it and just do not display anything there? And by not displaying anything I mean not displaying images from different cells?
You are reusing your cells, thus the cell will still use the previous image that it has loaded unless you set it to nil or a placeholder image. However I believe you do not need to use an if statement. You can use the placeholderImage parameter of af_setImageWithURL.
cell.myPhoto.af_setImageWithURL(NSURL(string: test.photo)!, placeholderImage: image)
Using parse.com v1.4 (installed with cocoa pods) with Swift 2 +
I've setup up my PFQueryTableViewController (ParseUI) with a custom cell. I believe everything is hooked up fine, however I can't get the cell to display the value from Parse.com even though I do receive it and can print it (I've debugged it).
I am dequeueing like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! OrderTableViewCell
// Extract values from the PFObject to display in the table cell
if let pfObject = object {
// prints expected value, e.g. 1, 2, 3
print(pfObject["table"])
// shows "Label" which is my label's default text on the SB!!
let table = pfObject["table"] as? String
cell.lblTable.text = table
}
return cell
}
The rest of my PFQueryTableViewController which seems to load data OK from Parse:
My custom cell OrderTableViewCell (with some label IBActions):
SB set-up:
And the result:
How can I get the label to display the actual text? What am I missing? I don't want to revert back to standard UITableView due to the additional features I get with Parse UI.
turns out it was as simple as changing
let table = pfObject["table"] as? String
to
let table = String(pfObject["table"])
I'm using Parse. I have a column in my table to store images as PFFiles. I am using a PFQueryTableViewController. I want to display images in my Parse table's "image" column as thumbnails for each table view cell that has an image associated with it.
Here is the relevant cellForRowAtIndexPath code I'm suspicious of:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("ListingCell", forIndexPath: indexPath) as PFTableViewCell
// This works as you can see "grapes" displayed on the screenshot I added.
var label = cell.viewWithTag(2) as UILabel
label.text = object["title"] as? String
// Accessing the PFImageView within my custom PFTableViewCell via a tag
let imageView = cell.viewWithTag(3) as PFImageView
// Extracting the image stored as a PFFile stored in the database
imageView.file = object["image"] as? PFFile // remote image
// Setting the actual thumbnail with the image when loaded
imageView.loadInBackground { (image: UIImage!, error: NSError!) -> Void in
imageView.image = image
}
return cell
}
I have 2 records stored in this Parse table, the first does not have an image and the second one does (hence why the "grapes" cell is empty). My question is, why is the image not displaying in the thumbnail (properly constrained) I created and rather taking up the entire screen? What am I doing wrong here? The even weirder part is that I used Xcode's view debugging to capture the view hierarchy and it shows it properly placed within the thumbnail. Any ideas?
You need to make sure your image mode is set to something like "Scale To Fill".