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

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
}

Related

Subtitles wont display on table views in Swift 5 despite being a subtitled table view cell

I'm working on a project that needs to display two table views at once that each draw from their own class, but no matter what I try, I can't get subtitles to display. The table view cells are formatted to subtitle, and it recognizes detailed text labels, but it just displays only the main text, or just an empty cell.
-------main story board-------
import UIKit
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableViewOne: UITableView!
#IBOutlet weak var eraserSwitch: UISwitch!
#IBOutlet weak var tableViewTwo: UITableView!
var topShelf: [Top] = []
var bottomShelf: [Bottom] = []
//var topShelf = ["Milk", "Eggs", "Yogurt"]
//var bottomShelf = ["Lettuce", "Carrots", "Onions"]
override func viewDidLoad() {
super.viewDidLoad()
topShelf.append(T1)
topShelf.append(T2)
bottomShelf.append(B1)
bottomShelf.append(B2)
// Do any additional setup after loading the view.
tableViewOne.register(UITableViewCell.self, forCellReuseIdentifier: "cellIDOne")
tableViewOne.delegate = self
tableViewOne.dataSource = self
tableViewTwo.register(UITableViewCell.self, forCellReuseIdentifier: "cellIDTwo")
tableViewTwo.delegate = self
tableViewTwo.dataSource = self
}
#IBAction func switchPressed(_ sender: UISwitch) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableViewOne {
return topShelf.count
}
return bottomShelf.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tableViewOne {
let cell = tableViewOne.dequeueReusableCell(withIdentifier: "cellIDOne", for: indexPath)
// cell.textLabel?.text = topShelf[indexPath.row].description
cell.textLabel?.text = topShelf[indexPath.row].item
//cell.textLabel?.text = "a"
// cell.detailTextLabel?.text = topShelf[indexPath.row].experationDate
//cell.detailTextLabel?.text = "a"
cell.detailTextLabel?.text = "v"
//print(topShelf[indexPath.row].experationDate)
return cell
} else {
let cell = tableViewTwo.dequeueReusableCell(withIdentifier: "cellIDTwo", for: indexPath)
// cell.textLabel?.text = bottomShelf[indexPath.row]
cell.textLabel?.text = bottomShelf[indexPath.row].item
cell.detailTextLabel?.text = bottomShelf[indexPath.row].experationDate
//print(bottomShelf[indexPath.row].experationDate)
//USE THIS CODE AS A SKELETON FOR ADDING, ERASING, AND MARKING
// if playlist[indexPath.row].Favorite == true {
// cell.detailTextLabel?.text = "\(playlist[indexPath.row].artistName) ♥"
// } else {
// cell.detailTextLabel?.text = "\(playlist[indexPath.row].artistName) \(playlist[indexPath.row].playCount)"
// }
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == tableViewOne {
// first table clicked, use for eraser
tableViewOne.deselectRow(at: indexPath, animated: true)
print("clickOne")
} else {
// you clicked the second table
tableViewTwo.deselectRow(at: indexPath, animated: true)
print("clickTwo")
}
}
-------Class-------
import Foundation
class Top {
var item: String
var retrievalDate: String
var experationDate: String
init(item: String, retrievalDate: String, experationDate: String) {
self.item = item
self.retrievalDate = retrievalDate
self.experationDate = experationDate
}
}
class Bottom {
var item: String
var retrievalDate: String
var experationDate: String
init(item: String, retrievalDate: String, experationDate: String) {
self.item = item
self.retrievalDate = retrievalDate
self.experationDate = experationDate
}
}
let T1 = Top(item: "Milk", retrievalDate: "10/11/22", experationDate: "10/22/22")
let T2 = Top(item: "Yogurt", retrievalDate: "10/11/22", experationDate: "10/20/22")
let B1 = Bottom(item: "Carrots", retrievalDate: "10/11/22", experationDate: "11/1/22")
let B2 = Bottom(item: "Lettuce", retrievalDate: "10/11/22", experationDate: "12/3/22")

Displaying the Image From API using DidSelectRow

I am trying to send the data in including image from one view controller to another . The data is fetching from API . The data is successfully loaded into first view controller including image but when I try to reuse same code with didSelectRow function I am getting following errors . Cannot assign value of type 'Data?' to type 'UIImage' . The error on this line dc.imagemovie = presenter.getImageData(by: row). Here is the code to fetch the data from API.
class MoviePresenter: MoviePresenterProtocol {
private let view: MovieViewProtocol
private let networkManager: NetworkManager
private var movies = [Movie]()
private var cache = [Int: Data]()
var rows: Int {
return movies.count
}
init(view: MovieViewProtocol, networkManager: NetworkManager = NetworkManager()) {
self.view = view
self.networkManager = networkManager
}
func getMovies() {
let url = "https://api.themoviedb.org/3/movie/popular?language=en-US&page=3&api_key=6622998c4ceac172a976a1136b204df4"
networkManager.getMovies(from: url) { [weak self] result in
switch result {
case .success(let response):
self?.movies = response.results
self?.downloadImages()
DispatchQueue.main.async {
self?.view.resfreshTableView()
}
case .failure(let error):
DispatchQueue.main.async {
self?.view.displayError(error.localizedDescription)
}
}
}
}
func getTitle(by row: Int) -> String? {
return movies[row].originalTitle
}
func getOverview(by row: Int) -> String? {
return movies[row].overview
}
func getImageData(by row: Int) -> Data? {
return cache[row]
}
private func downloadImages() {
let baseImageURL = "https://image.tmdb.org/t/p/w500"
let posterArray = movies.map { "\(baseImageURL)\($0.posterPath)" }
let group = DispatchGroup()
group.enter()
for (index, url) in posterArray.enumerated() {
networkManager.getImageData(from: url) { [weak self] data in
if let data = data {
self?.cache[index] = data
}
}
}
group.leave()
group.notify(queue: .main) { [weak self] in
self?.view.resfreshTableView()
}
}
}
Here is the code in view controller to display the data into table view cell .
class MovieViewController: UIViewController {
#IBOutlet weak var userName: UILabel!
#IBOutlet weak var tableView: UITableView!
private var presenter: MoviePresenter!
var finalname = ""
override func viewDidLoad() {
super.viewDidLoad()
userName.text = "Hello: " + finalname
setUpUI()
// configure presenter
presenter = MoviePresenter(view: self)
presenter.getMovies()
}
private func setUpUI() {
tableView.dataSource = self
tableView.delegate = self
}
#IBAction func selectSegment(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0{
setUpUI()
presenter = MoviePresenter(view: self)
presenter.getMovies()
}
}
}
extension MovieViewController: MovieViewProtocol {
func resfreshTableView() {
tableView.reloadData()
}
func displayError(_ message: String) {
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
let doneButton = UIAlertAction(title: "Done", style: .default, handler: nil)
alert.addAction(doneButton)
present(alert, animated: true, completion: nil)
}
}
extension MovieViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
presenter.rows
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MovieViewCell.identifier, for: indexPath) as! MovieViewCell
let row = indexPath.row
let title = presenter.getTitle(by: row)
let overview = presenter.getOverview(by: row)
let data = presenter.getImageData(by: row)
cell.configureCell(title: title, overview: overview, data: data)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let dc = storyboard?.instantiateViewController(withIdentifier: "MovieDeatilsViewController") as! MovieDeatilsViewController
let row = indexPath.row
dc.titlemovie = presenter.getTitle(by: row) ?? ""
dc.overview = presenter.getOverview(by: row) ?? ""
**dc.imagemovie = presenter.getImageData(by: row)**
self.navigationController?.pushViewController(dc, animated: true)
}
}
extension MovieViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}
Here is the code for display the data .
class MovieDeatilsViewController: UIViewController {
#IBOutlet weak var movieImage: UIImageView!
#IBOutlet weak var movieTitle: UILabel!
#IBOutlet weak var movieOverview: UILabel!
var titlemovie = ""
var overview = ""
var imagemovie = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
movieTitle.text = titlemovie
movieOverview.text = overview
movieImage.image = imagemovie
}
}
You need to return UIImage :
class MoviePresenter: MoviePresenterProtocol {
...
// Convert data to UIImage
func getImageData(by row: Int) -> UIImage? {
return UIImage(data: cache[row])
}
You need yo use UIImage and UIImage view to configure cell :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MovieViewCell.identifier, for: indexPath) as! MovieViewCell
let row = indexPath.row
let title = presenter.getTitle(by: row)
let overview = presenter.getOverview(by: row)
let image = presenter.getImageData(by: row)
// cell is now configured with an image
cell.configureCell(title: title, overview: overview, image: image)
return cell
}
You need to modify cell.configureCell to handle UIImage? instead of data.
When selecting a cell you must use UIImage to init VC :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let dc = storyboard?.instantiateViewController(withIdentifier: "MovieDeatilsViewController") as! MovieDeatilsViewController
let row = indexPath.row
dc.titlemovie = presenter.getTitle(by: row) ?? ""
dc.overview = presenter.getOverview(by: row) ?? ""
dc.imagemovie = presenter.getImageData(by: row)// now an image
self.navigationController?.pushViewController(dc, animated: true)
}
Use UIImage to initialise image movie in detail vc :
class MovieDeatilsViewController: UIViewController {
#IBOutlet weak var movieImageView: UIImageView! // Image view here
#IBOutlet weak var movieTitle: UILabel!
#IBOutlet weak var movieOverview: UILabel!
var titlemovie = ""
var overview = ""
var imagemovie : UIImage?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated: animated)
movieTitle.text = titlemovie
movieOverview.text = overview
// here you could also display a default image
// if image is not set
if let image = imagemovie {
movieImageView.image = image
}
}
}

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)
}
}

Pass value to next view when label in Table view cell is tapped

would love to pass the value postArray[indexpath.row].creatorId when the label inside a tableview cell is tapped so it can be passed onto the next view controller so i can load the profile of that particular creator/user. I used custom cells, so how do i get the creator id based on the location of the label(username) selected.
//custom cell
class PostCell : UITableViewCell
{
#IBOutlet weak var timeAgoLabel: UILabel!
#IBOutlet weak var usernameLabel: UILabel!
#IBOutlet weak var profileImageView: UIImageView!
#IBOutlet weak var postImageView: UIImageView!
#IBOutlet weak var captionLabel: UILabel!
#IBOutlet weak var postStatsLabel: UILabel!
}
//do something when label is tapped
#objc func tapFunction(sender:UITapGestureRecognizer) {
//userClicked = creatorData
print(userClicked)
appDelegate.profileView()
print("tap working")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0{
return 1
}else{
return postsArray.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//var returnCell: UITableViewCell!
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as! statusCell
profilePicture = UserDefaults.standard.object(forKey: "userPic") as? String
if profilePicture != nil {
//load profile picture from library
let urlString = "https://test.com/uploads/profile-picture/"+(profilePicture)!
let profileURL = URL(string: urlString)
cell.statusProfilePic?.downloadedFrom(url: profileURL!)
} else {
print("you have no profile picture set")
}
return cell
} else {
if postsArray[indexPath.row].photos != nil{
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostCell
if postsArray[indexPath.row].comments != nil {
comments = postsArray[indexPath.row].comments?.count
} else {
comments = 0
}
if postsArray[indexPath.row].like_list != nil {
likes = postsArray[indexPath.row].like_list?.count
}else{
likes = 0
}
//assign post id to PostID
postID = postsArray[indexPath.row].post_id
//make username clickable!
let tap = UITapGestureRecognizer(target: self, action: #selector(NewsfeedTableViewController.tapFunction))
cell.usernameLabel.isUserInteractionEnabled = true
cell.usernameLabel.addGestureRecognizer(tap)
cell.usernameLabel.text = postsArray[indexPath.row].fullname
cell.timeAgoLabel.text = postsArray[indexPath.row].data_created
cell.captionLabel.text = postsArray[indexPath.row].content
cell.timeAgoLabel.text = postsArray[indexPath.row].modified
//15 Likes 30 Comments 500 Shares
cell.postStatsLabel.text = "\(likes!) Likes \(comments!) Comments"
//load profile picture from library
let urlString = "https://test.com/uploads/profile-picture/"+(postsArray[indexPath.row].profile_pic_filename)!
let profileURL = URL(string: urlString)
cell.profileImageView.downloadedFrom(url: profileURL!)
//iterate through posts images images array
//load post picture from server library
var postImageName : String?
if postsArray[indexPath.row].photos != nil{
let postImage = postsArray[indexPath.row].photos
for postsImage in postImage!{
postImageName = postsImage.filename!
}
let urlPostImageString = "https://test.com/uploads/post-picture/"+(postImageName)!
let postsImageUrl = URL(string: urlPostImageString)
cell.postImageView.downloadedFrom(url: postsImageUrl!)
} else {
print("Post has no picture")
}
//return cell
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "NoImageCell", for: indexPath) as! NoImageTableViewCell
if postsArray[indexPath.row].comments != nil {
comments = postsArray[indexPath.row].comments?.count
} else {
comments = 0
}
if postsArray[indexPath.row].like_list != nil {
likes = postsArray[indexPath.row].like_list?.count
} else {
likes = 0
}
//make username clickable!
let tap = UITapGestureRecognizer(target: self, action: #selector(NewsfeedTableViewController.tapFunction))
cell.noImageUsername.isUserInteractionEnabled = true
cell.noImageUsername.addGestureRecognizer(tap)
cell.noImageUsername.text = postsArray[indexPath.row].fullname
cell.noImageTime.text = postsArray[indexPath.row].data_created
cell.noImagePost.text = postsArray[indexPath.row].content
cell.noImageTime.text = postsArray[indexPath.row].modified
//15 Likes 30 Comments 500 Shares
cell.noImageLikeAndComment.text = "\(likes!) Likes \(comments!) Comments"
//load profile picture from library
let urlString = "https://test.com/uploads/profile-picture/"+(postsArray[indexPath.row].profile_pic_filename)!
let profileURL = URL(string: urlString)
cell.noImageProfilePic.downloadedFrom(url: profileURL!)
return cell
}
}
}
Use this for example.
Implement didSelectRow() method and in it write something like this:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// this method works, when you taped cell. write here code of you need. Next code only example, which set user info to some VC and push it:
let controller = UserController as? UserController
if let controller = controller {
controller.user = users[indexPath.row]
self.navigationController?.pushViewController(controller, animated: true)
}
}
add this to your Cell's class:
func setTap() {
let tap = UITapGestureRecognizer(target: self, action: #selector(tapRecognized))
self.label.addGestureRecognizer(tap)
tap.numberOfTapsRequired = 1
}
#objc func tapRecognized(sender: UITapGestureRecognizer) {
// here your code of tap on label
print("label tapped")
}
Check on storyBoard is your label isUserInteractionEnabled? - set it to true. Inside tapRecodnized() method do what are you need. And you need to call method setTap() in your cell's method, which you call in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell.
Update
Simple example. this code know what are you tapped. if you tap cell, but not label, add code of push some controller, else code of push another controller.
Cell's Class:
class MyTableViewCell: UITableViewCell {
#IBOutlet weak var label: UILabel!
var mainController: ViewController?
func setText(text: String) {
setTap()
label.text = text
}
func setTap() {
let tap = UITapGestureRecognizer(target: self, action: #selector(tapRecognized))
self.label.addGestureRecognizer(tap)
tap.numberOfTapsRequired = 1
}
#objc func tapRecognized(sender: UITapGestureRecognizer) {
if let mainController = mainController {
print("label tapped")
mainController.pushSomeVc(cell: self)
}
}
}
Code of main Class:
class ViewController: UIViewController {
#IBOutlet weak var myTableView: UITableView!
var array = ["1", "2", "3", "4", "5", "6"]
override func viewDidLoad() {
super.viewDidLoad()
}
func pushSomeVc(cell: MyTableViewCell) {
let row = myTableView.indexPath(for: cell)?.row
if let row = row {
// write here code of push controller, when label tapped. row property for get some user from array
print("push some vc with \(row)")
}
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "cell") as? MyTableViewCell
if let cell = cell {
cell.setText(text: array[indexPath.row])
cell.mainController = self
}
return cell ?? UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
// write here code of push controller with comments
print("cell tapped: \(indexPath.row)")
}
}
I tested this code and it's work perfect

Pass data between tableViews by using storyboard instantiateviewcontrollerwithidentifier

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")

Resources