Pass data between tableViews by using storyboard instantiateviewcontrollerwithidentifier - ios

I'm trying to pass data from one tableView to to a static one. I'm not using a segue and using instantiateviewcontrollerwithidentifier instead, i dont know where i'm going wrong here. I'm using cloudkit and fetching ckRecords from icloud. i have a collectionView first where i tap a cell and displays books corresponding to that cell. However when they are displayed in the cells, i want to tap on the cell to bring up the detailViewController. It's not working at the moment and can't get the head around why.
import CloudKit
class DetailViewController: UITableViewController {
#IBOutlet weak var aboutBookLabel: UILabel!
#IBOutlet weak var bookLengthLabel: UILabel!
#IBOutlet weak var amazonPriceLabel: UILabel!
#IBOutlet weak var ebayPriceLabel: UILabel!
#IBOutlet weak var websitePriceLabel: UILabel!
#IBOutlet weak var authorLabel: UILabel!
#IBOutlet weak var bookTitleLabel: UILabel!
#IBOutlet weak var bookImageView: UIImageView!
var bookRecord: CKRecord?
override func viewDidLoad() {
super.viewDidLoad()
populateData()
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor(red: 27.0/255.0, green: 28.0/255.0 , blue: 32.0/255.0, alpha: 1.0)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
func populateData() {
aboutBookLabel.text = bookRecord?.object(forKey: "description") as? String
bookLengthLabel.text = bookRecord?.object(forKey: "length") as? String
amazonPriceLabel.text = bookRecord?.object(forKey: "amazon") as? String
ebayPriceLabel.text = bookRecord?.object(forKey: "ebay") as? String
websitePriceLabel.text = bookRecord?.object(forKey: "authorPrice") as? String
authorLabel.text = bookRecord?.object(forKey: "author") as? String
bookTitleLabel.text = bookRecord?.object(forKey: "name") as? String
if let imageFile = bookRecord?.object(forKey: "image") as? CKAsset {
bookImageView.image = UIImage(contentsOfFile: imageFile.fileURL.path)
}
}
}
class BooksViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
let cellIdentifier = "bookCell"
var books = [CKRecord]()
var sectorTitle = "language"
override func viewDidLoad() {
super.viewDidLoad()
registerNib()
fetchBooksFromCloud()
configureTableView()
}
func fetchBooksFromCloud() {
books = [CKRecord]()
books.removeAll()
tableView.reloadData()
let cloudContainer = CKContainer.default()
let publicDatabase = cloudContainer.publicCloudDatabase
let predicate = buildBookPredicate()
let query = CKQuery(recordType: "Book", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["name", "author", "image", "format", "description", "length", "amazon", "ebay", "authorPrice"]
queryOperation.queuePriority = .veryHigh
queryOperation.resultsLimit = 25
queryOperation.recordFetchedBlock = { (record) -> Void in
self.books.append(record)
}
DispatchQueue.global(qos: .userInteractive).async {
queryOperation.queryCompletionBlock = { (cursor, error) -> Void in
if let error = error {
print("Download failed \(error.localizedDescription)")
return
}
print("Download Successful")
OperationQueue.main.addOperation {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
queryOperation.qualityOfService = .userInteractive
publicDatabase.add(queryOperation)
}
private func buildBookPredicate() -> NSPredicate {
return NSPredicate(format:"self contains '\(sectorTitle)'")
}
extension BooksViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return books.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier , for: indexPath) as! BookCell
let book = books[indexPath.row]
let bookName = book.object(forKey: "name") as? String
let authorName = book.object(forKey: "author") as? String
let image = book.object(forKey: "image") as? CKAsset
let formatName = book.object(forKey: "format") as? String
cell.nameLabel.text = bookName
cell.authorLabel.text = authorName
cell.formatLabel.text = formatName
cell.bookImageName.image = UIImage(contentsOfFile: image!.fileURL.path)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let record = books[indexPath.row]
if let detailVC = storyboard?.instantiateViewController(withIdentifier: "DetailVC") as? DetailViewController {
detailVC.bookRecord = record
navigationController?.pushViewController(detailVC, animated: true)
}
tableView.deselectRow(at: indexPath, animated: true)
}

The storyboard property of UIViewController represents the storyboard from which the view controller originated. If you want to load another view controller from the same storyboard you can use it.
However, if you want to load the UIViewController from another storyboard you have to create the UIStoryboard instance first.
You can do it like this.
UIStoryboard(name: "YourStoryboardName", bundle: nil).instantiateViewController(withIdentifier: "DetailVC")

Related

Class 'TableViewController' has no initializers [duplicate]

This question already has answers here:
Class 'ViewController' has no initializers in swift
(8 answers)
Closed last month.
import UIKit
import CoreData
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var titleArray = [String]()
var idArray = [UUID()]
var chosenTitle = ""
var chosenTitleID : UUID
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(addButtonClicked))
tableView.delegate = self
tableView.dataSource = self
getData()
}
func getData() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Places")
request.returnsObjectsAsFaults = false
do {
let results = try context.fetch(request)
if results.count > 0 {
self.titleArray.removeAll(keepingCapacity: false)
self.idArray.removeAll(keepingCapacity: false)
for result in results as! [NSManagedObject] {
if let title = result.value(forKey: "title") as? String {
self.titleArray.append(title)
}
if let id = result.value(forKey: "id") as? UUID {
self.idArray.append(id)
}
tableView.reloadData()
}
}
} catch {
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = titleArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleArray.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
chosenTitle = titleArray[indexPath.row]
chosenTitleID = idArray[indexPath.row]
performSegue(withIdentifier: "toDetailsVC", sender: nil)
}
I have a problem with Swift class. I have a swift file for UITableViewController class. I had "Class 'TableViewController' has no initializers" problem.
I didn't find anything but the code says TableViewController has no initializers. I don't understand this problem. I have version 14.2.
Thanks for your responses.
You can make chosenTitleID property optional(var chosenTitleID: UUID?) or provide the default value to chosenTitleID because TableViewController class contains non-optional property.

some problem about to show imageView from CoreData in TableView

I saved the photo and name of the movie to CoreData. entity name is movieEntity
When I try to show a photo and name of a movie in tableView, I have a problem with the picture. The name of the movie successfully visible to tableView, but the photo of the movie is not visible.
class movieViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
var movie: [NSManagedObject] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "movieEntity")
do {
coreDataMoviePhoto = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
}
extension movieViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return movie.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let movieData = movie[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "movieTableViewCell", for: indexPath) as! movieTableViewCell
cell.tableViewCellImageView.image = movieData.value(forKeyPath: "img") as? UIImage
cell.tableViewTextLabel.text = movieData.value(forKeyPath: "name") as? String
return cell
}
}
here is movieTableViewCell
class CoreDataWatchlistTableViewCell: UITableViewCell {
#IBOutlet weak var tableViewTextLabel: UILabel!
#IBOutlet weak var tableViewCellImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
}
As you saving image data not image in so use
if let data = movieData.value(forKeyPath: "img") as? Data{
cell.tableViewCellImageView.image = UIImage(data:data)
}else{
cell.tableViewCellImageView.image = somePlaceholderImage
}
hope its help

How pass the value of a selected cell to another ViewController?

Essentially, I have the following UITableViewController that contains custom tableView cells with labels in them. When the cell is selected I would like the value of the cell to be passed to the next view controller where I am using it in an HTTP POST response.
What can be added to didSelectRowAt to pass the value of the selected cell to the view controller presented?
Perhaps as a variable?
The following is my code:
import UIKit
class ScheduledCell: UITableViewCell {
#IBOutlet weak var ETALabel: UILabel!
#IBOutlet weak var cellStructure: UIView!
#IBOutlet weak var scheduledLabel: UILabel!
#IBOutlet weak var testingCell: UILabel!
#IBOutlet weak var pickupLabel: UILabel!
#IBOutlet weak var deliveryLabel: UILabel!
#IBOutlet weak var stopLabel: UILabel!
#IBOutlet weak var topBar: UIView!
}
class ToCustomerTableViewController: UITableViewController, UIGestureRecognizerDelegate {
var typeValue = String()
var driverName = UserDefaults.standard.string(forKey: "name")!
var structure = [AlreadyScheduledStructure]()
override func viewDidLoad() {
super.viewDidLoad()
fetchJSON()
//Disable delay in button tap
self.tableView.delaysContentTouches = false
tableView.tableFooterView = UIView()
}
private func fetchJSON() {
guard let url = URL(string: "https://example.com/example/example"),
let value = driverName.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)
else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = "driverName=\(value)".data(using: .utf8)
URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data else { return }
do {
self.structure = try JSONDecoder().decode([AlreadyScheduledStructure].self,from:data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
catch {
print(error)
}
}.resume()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return structure.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "scheduledID", for: indexPath) as! ScheduledCell
let portfolio = structure[indexPath.row]
cell.stopLabel.text = "Stop \(portfolio.stop_sequence)"
cell.testingCell.text = portfolio.customer
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let portfolio = structure[indexPath.row]
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "scheduledDelivery")
print(portfolio.customer)
controller.navigationItem.title = navTitle
navigationController?.pushViewController(controller, animated: true)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200.0
}
}
Create public variables for the data which you want to pass to the scheduledDelivery controller.Then set them inside didselect delegate method. Let say if you want to pass portfolio.customer. Declare following public variable on scheduledDelivery controller.
public var portfilio:String?
Then set value to that variable from the didselect method like this,
let portfolio = structure[indexPath.row]
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "scheduledDelivery")
controller.portfilio = portfolio.customer
controller.navigationItem.title = navTitle
navigationController?.pushViewController(controller, animated: true)
add a portfolio variable to your next ViewController
class scheduledDeleivery: UIViewController{
var customer:String? //suposing this is customer type
then in
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let portfolio = structure[indexPath.row]
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "scheduledDelivery") as! shcheduledDeleivery
controller.customer = porfolio.customer //here is the customer you need to pass to next viewcontroller
print(portfolio.customer)
controller.navigationItem.title = navTitle
navigationController?.pushViewController(controller, animated: true)
}
You can store the cell's data as a variable and then in prepare for segue function pass it to the other ViewController. If you call this in prepare for segue it will automatically do it every time you try to access that segue.
var nameOfVar : String = ""
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var secondVC = segue.destination as! YourSecondViewController
secondVC.variable = nameOfVar
}
Hope I've helped :)

pass image to another view when clicked on tableview cell. The image is fetched from json data and initialised with alamofire

i am fetching json data from url and displaying it in tableview. I want to image in tableview to another view controller when clicked on tableview cell. My another label are showing but dont know how to write code for image in didselectrowatindexpath method of tableview
my code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
//getting hero for specified position
let hero: Hero
hero = heroes[indexPath.row]
//displaying values
cell.labelName.text = hero.name
cell.labelTeam.text = hero.team
cell.labelRealName.text = hero.realname
cell.labelAppear.text = hero.firstappearance
cell.labelPublish.text = hero.publisher
//displaying image
Alamofire.request(hero.imageUrl!).responseImage { (response) in
debugPrint(response)
if let image = response.result.value {
cell.heroImage.image = image
}
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let heroesDetails:HeroesDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "HeroesDetailsViewController") as! HeroesDetailsViewController
heroesDetails.strlabelTeam = heroes[indexPath.row].team
heroesDetails.strlabelName = heroes[indexPath.row].name
heroesDetails.strlabelAppear = heroes[indexPath.row].firstappearance
heroesDetails.strlabelPublish = heroes[indexPath.row].publisher
heroesDetails.strlabelRealName = heroes[indexPath.row].realname
self.navigationController?.pushViewController(heroesDetails, animated: true)
}
my heroesdetailviewcontroller file where i want to display code:
import UIKit
class HeroesDetailsViewController: UIViewController {
#IBOutlet weak var detailsImg: UIImageView!
#IBOutlet weak var detailsName: UILabel!
#IBOutlet weak var detailsRealName: UILabel!
#IBOutlet weak var detailsTeam: UILabel!
#IBOutlet weak var detailsAppear: UILabel!
#IBOutlet weak var detailsPublisher: UILabel!
var strheroImage: UIImage!
var strlabelName: String!
var strlabelTeam: String!
var strlabelAppear: String!
var strlabelPublish: String!
var strlabelRealName: String!
override func viewDidLoad() {
super.viewDidLoad()
detailsImg.image = strheroImage
detailsName.text = strlabelName
detailsRealName.text = strlabelRealName
detailsTeam.text = strlabelTeam
detailsAppear.text = strlabelAppear
detailsPublisher.text = strlabelPublish
// Do any additional setup after loading the view.
}
}
my modal file:
class Hero{
var name:String?
var team:String?
var imageUrl:String?
var realname:String?
var firstappearance:String?
var publisher:String?
init(name:String?, team:String?, imageUrl:String?, realname:String?, firstappearance:String?, publisher:String?) {
self.name = name
self.team = team
self.imageUrl = imageUrl
self.realname = realname
self.firstappearance = firstappearance
self.publisher = publisher
}
}
my tableviewcell.swift file:
import UIKit
class TableViewCell: UITableViewCell {
#IBOutlet weak var heroImage: UIImageView!
#IBOutlet weak var labelName: UILabel!
#IBOutlet weak var labelTeam: UILabel!
#IBOutlet weak var labelAppear: UILabel!
#IBOutlet weak var labelPublish: UILabel!
#IBOutlet weak var labelRealName: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
my main viewcontroller.swift file which contains all code:
import UIKit
import Alamofire
import AlamofireImage
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
//MARK: IBOUTLETS
#IBOutlet weak var tableviewHeroes: UITableView!
// Web API Url
let URL_GET_DATA = "https://simplifiedcoding.net/demos/marvel/"
// List to store Heroes
var heroes = [Hero]()
//implementing uirefreshcontrol to tableview
lazy var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(ViewController.handleRefresh), for: .valueChanged)
refreshControl.tintColor = UIColor.init(red: 217/255, green: 133/255, blue: 199/255, alpha: 1)
return refreshControl
}()
override func viewDidLoad() {
super.viewDidLoad()
self.tableviewHeroes.addSubview(self.refreshControl)
//fetching data from web api
Alamofire.request(URL_GET_DATA).responseJSON { (response) in
//getting json
if let json = response.result.value {
//converting json to NSArray
let heroesArray:NSArray = json as! NSArray
//traversing through all elements of the array
for i in 0..<heroesArray.count {
//adding heroes value to hero list
self.heroes.append(Hero(
name: (heroesArray[i] as AnyObject).value(forKey: "name") as? String, team: (heroesArray[i] as AnyObject).value(forKey: "team") as? String, imageUrl: (heroesArray[i] as AnyObject).value(forKey: "imageurl") as? String,
realname: (heroesArray[i] as AnyObject).value(forKey: "realname") as? String, firstappearance: (heroesArray[i] as AnyObject).value(forKey: "firstappearance") as? String, publisher: (heroesArray[i] as AnyObject).value(forKey: "publisher") as? String ))
}
//display data in tableview
self.tableviewHeroes.reloadData()
}
}
self.tableviewHeroes.reloadData()
}
func handleRefresh(refreshControl: UIRefreshControl) {
self.tableviewHeroes.reloadData()
refreshControl.endRefreshing()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return heroes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
//getting hero for specified position
let hero: Hero
hero = heroes[indexPath.row]
//displaying values
cell.labelName.text = hero.name
cell.labelTeam.text = hero.team
cell.labelRealName.text = hero.realname
cell.labelAppear.text = hero.firstappearance
cell.labelPublish.text = hero.publisher
//displaying image
Alamofire.request(hero.imageUrl!).responseImage { (response) in
debugPrint(response)
if let image = response.result.value {
cell.heroImage.image = image
}
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let heroesDetails:HeroesDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "HeroesDetailsViewController") as! HeroesDetailsViewController
let hero: Hero
hero = heroes[indexPath.row]
let image : UIImage = UIImage(data: hero.imageUrl)
heroesDetails.strlabelTeam = heroes[indexPath.row].team
heroesDetails.strlabelName = heroes[indexPath.row].name
heroesDetails.strlabelAppear = heroes[indexPath.row].firstappearance
heroesDetails.strlabelPublish = heroes[indexPath.row].publisher
heroesDetails.strlabelRealName = heroes[indexPath.row].realname
heroesDetails.strheroImage = image
self.navigationController?.pushViewController(heroesDetails, animated: true)
}
}
You should use Caches policy instead of passing downloaded image from one VC to another. Larger image could take time to download, user can not wait for it before tapping the table view cell.
For more details please see Image Cache Section
https://github.com/Alamofire/AlamofireImage
In your didSelectRowAt indexPath function, before sending image to different viewController try to convert the data into image first, then send it:
First declare a global image variable:
var imageArray = [UIImage]()
Then assign the image you get from alamofireImage to this variable in your cellForRowAt indexPath function:
if let image = response.result.value {
cell.heroImage.image = image
self.imageArray.append(image)
}
Then pass it :
heroesDetails.strlabelTeam = heroes[indexPath.row].team
heroesDetails.strlabelName = heroes[indexPath.row].name
heroesDetails.strlabelAppear = heroes[indexPath.row].firstappearance
heroesDetails.strlabelPublish = heroes[indexPath.row].publisher
heroesDetails.strlabelRealName = heroes[indexPath.row].realname
heroesDetails.strheroImage = self.imageArray[indexPath.row]
1st View Controller:
import UIKit
import Alamofire
import ObjectMapper
var homeScreenData = [HomeDataModel]()
func homeScreenDataAPI(){
var params : Parameters?
params =
[
"user_id" : id ?? 0,
]
print(params as Any)
self.view.makeToastActivity(.center)
ApiCallWithHeader(url : “homeDataAPI” , params : params!) { responseObject, error in
// use responseObject and error here
print("responseObject = \(String(describing: responseObject)); error = \(String(describing: error))")
let JSON = responseObject
if(JSON?["status"] as? String == "success") {
if let responseData = (JSON?["data"] as? [Dictionary<String, AnyObject>]) {
if let homeScreenDataValue = Mapper<HomeDataModel>().mapArray(JSONObject: responseData)
{
self.homeScreenData = homeScreenDataValue
}
}
return
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
vc.homeScreenData = homeScreenData
vc.indexTagValue = indexPath.item
self.navigationController?.pushViewController(vc, animated: true)
}
Second View Controller:-
var homeScreenData = [HomeDataModel]()
var indexTagValue = 0
extension HomeScreenDetailViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return homeScreenData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : HomeDataOtherCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "testCollectionViewCell", for: indexPath as IndexPath) as! testCollectionViewCell
cell.nameLabel.text = homeScreenData[indexPath.item].name
if let image = homeScreenData[indexPath.item].user_image {
if image.contains("https://") {
cell.userImageView.sd_setImage(with: URL(string: image ), placeholderImage: UIImage(named: "userIcon"))
} else {
let userImage = ImageURL + image
// userImageView.image = UIImage(named: userImage )
let urlString = userImage.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
cell.userImageView.sd_setImage(with: URL(string: urlString ?? ""), placeholderImage: UIImage(named: "userIcon"))
}
} else {
cell.userImageView.image = UIImage(named: "userIcon")
}
return cell
}
ImageURL in my app is
let ImageURL = "http://test/public/images/
It is recommended to pass the data model so that it can reduce a lot of unnecessary code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellid = "testCellID"
var cell = tableView.dequeueReusableCell(withIdentifier: cellid)
if cell==nil {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellid)
}
let hero: Hero
hero = heroes[indexPath.row] as! Hero
cell?.textLabel?.text = hero.name
let url = URL(string: hero.imageUrl ?? "")
let data = try! Data(contentsOf: url!)
cell?.imageView?.image = UIImage(data: data)
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let hero = heroes[indexPath.row] as! Hero
let heroesDetails = HeroesDetailsViewController()
heroesDetails.hero = hero
self.navigationController?.pushViewController(heroesDetails, animated: true)
}
class HeroesDetailsViewController: UIViewController {
#IBOutlet weak var detailsImg: UIImageView!
var hero: Hero!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: hero.imageUrl ?? "")
let data = try! Data(contentsOf: url!)
self.detailsImg?.image = UIImage(data: data)
}
}

Load/Download image from SDWebimage and display in Custom tableView cell imageView

i already parsed other values but how will i show image to imageView i m already using SdWebImage in Swift.I want to show that in Bottom cell "detail" Below is my code
import UIKit
import SystemConfiguration
import MBProgressHUD
public struct Section {
var arrayDataTop: String
var arrayTerms: String
var qrImage:String
var collapsed: Bool
public init( arrayDataTop: String,qrImage: String ,arrayTerms: String, collapsed: Bool = false) {
self.arrayDataTop = arrayDataTop
self.qrImage = qrImage
self.arrayTerms = arrayTerms
self.collapsed = collapsed
}
}
class CollapsibleViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet weak var tableViewCollapsible:UITableView!
#IBOutlet weak var listImage:UIImageView!
var nodatastr:String = "No Deal Found."
var dealIDCollapsible : String?
var dealDictCollapsible = [String: AnyObject]()
var parentNavigationController: UINavigationController?
private var loadingView:MBProgressHUD?
var sectionDataObj = [Section]()
override func viewDidLoad() {
super.viewDidLoad()
if (!self.isInternetAvailable()){
self.alertMessageShow(title: "No Internet Connection", message: "Make sure your device is connected to the internet.")
}
else{
if self.loadingView == nil {
self.loadingView = MBProgressHUD.showAdded(to: self.view, animated: true)
}
tableViewCollapsible.estimatedRowHeight = 100.0
tableViewCollapsible.layoutIfNeeded()
tableViewCollapsible.updateConstraintsIfNeeded()
tableViewCollapsible.tableFooterView = UIView()
self.listImageFetch()
dealFetchParticularListing()
}
}
func dealFetchParticularListing(){
let prs = [
"listing_id":dealIDCollapsible,//dealIDCollapsible,
"Deal_fetch_listing": "1" as String
]
Service.CreateDeal(prs as [String : AnyObject]?, onCompletion: { result in
let json = result as? NSDictionary
if let data = json as? [String:Any]{
if let err = data["status"] as? String, err == "success"{
if let data = data["result"] as? [Any]{
//
//fill your data in that local Section obj
//
var sectionDataObj = [Section]()
for sectionObj in data{
if let sectionObjVal = sectionObj as? [String:Any]{
if let qrcode = sectionObjVal["qrcode"] as? String{
if let tnc = sectionObjVal["tnc"] as? String{
if let deal_title = sectionObjVal["deal_title"] as? String{
let sectionValue = Section(arrayDataTop: deal_title, qrImage: qrcode, arrayTerms: tnc)
// access main objects/UIelement on main thread ONLY
sectionDataObj.append(sectionValue)
}
}
}
}
}
DispatchQueue.main.async { () -> Void in
self.sectionDataObj.removeAll()
//
//assign ur data in main sampleData(Section obj) then reload tableView with that data.
//
self.sectionDataObj = sectionDataObj
self.tableViewCollapsible.reloadData()
self.loadingView?.hide(true)
}
}
}
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Header
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "header") as! MyCellData
cell.lblDealTitle.text = sectionDataObj[indexPath.section].arrayDataTop
return cell
}
// Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "detail") as! MyCellData
cell.lblTerm.text = sectionDataObj[indexPath.section].arrayTerms
// here i want to show image
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension//320.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
let collapsed = !sectionDataObj[indexPath.section].collapsed
// Toggle collapse
sectionDataObj[indexPath.section].collapsed = collapsed
self.tableViewCollapsible.reloadSections([indexPath.section], with: .automatic)
}
}
}
class MyCellData:UITableViewCell{
#IBOutlet weak var lblDealTitle: UILabel!
#IBOutlet weak var dealimage: UIImageView!
#IBOutlet weak var lblTerm: UILabel!
#IBOutlet weak var qrCodeImage: UIImageView!
}
Plz help me with this.thanks in advance.All things are working properly i am able to see other details.Help will be appreciated.Plz help me i m struggling with this issue.
update ur tableView cellForRowAt like so to show an image or ur cell from ur sectionDataObj
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Header
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "header") as! MyCellData
cell.lblDealTitle.text = sectionDataObj[indexPath.section].arrayDataTop
return cell
}
// Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "detail") as! MyCellData
cell.lblTerm.text = sectionDataObj[indexPath.section].arrayTerms
let imgUrl = sectionDataObj[indexPath.section]. qrImage
cell.qrCodeImage.sd_setImage(with: URL(string:imgUrl), completed: nil)
return cell
}

Resources