How to make web view with preview in app page? - swift

I want show preview of web content in my app. When user click to the web view preview then it should go to the web-site.
Below my code
MainViewController.swift
import UIKit
import SDWebImage
import WebKit
class MainViewController: UIViewController, WebServiceDelegate, UITableViewDelegate, UITableViewDataSource, WKUIDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var indicator: UIActivityIndicatorView!
var tests: [Test] = []
let defaults = UserDefaults.standard
var webService = WebService()
var webView : WKWebView!
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
override func loadView() {
let webConfig = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfig)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com/")
let myReq = URLRequest(url: myURL!)
webView.load(myReq)
label.center = tableView.center
label.textAlignment = .center
label.text = ""
tableView.isHidden = true
indicator.isHidden = true
indicator.center = view.center
webService.delegate = self
tableView.dataSource = self
tableView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
HHTabBarView.shared.isHidden = false
navigationController?.navigationBar.isHidden = true
Where can I use content preview?
if (defaults.object(forKey: "AccessToken") == nil) {
if let destinationView = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "IntroViewController") as? IntroViewController {
if let navigator = self.navigationController {
navigator.pushViewController(destinationView, animated: false)
}
}
} else {
startLoading(withView: view, withIndicator: indicator)
webService.getTests()
I have backend parameters like
webService.getWebContents(withWebImage: "webImage", withWebTitle: "webTitle", withWebDescription: "webDescription", withWebCreater: "webCreater", withWebPubDate: "webPubdate")
}
}
I want to make a web view which is shown with preview in my
MainViewController's table views cells .
func configureCell(for cell: UITableViewCell, with item: Test) {
let testImage = cell.viewWithTag(1001) as! UIImageView
testImage.sd_setImage(with: URL(string: item.image), completed: nil)
testImage.layer.borderWidth = 0
testImage.layer.borderColor = UIColor.black.cgColor
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat(Utility.scaledHeight(320))
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tests.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "projectCell", for: indexPath)
cell.selectionStyle = .none
let item = tests[indexPath.row]
configureCell(for: cell, with: item)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let destinationView = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TestDetailViewController") as? TestDetailViewController {
if let navigator = self.navigationController {
navigator.pushViewController(destinationView, animated: true)
destinationView.testId = tests[indexPath.row].id
}
}
}
}

I had a navigation view on the top of the webView. Cancelled this little view. Added normal UIButton item with back arrow. navigationController?.popToRootViewController(animated:true)

Related

Reloading table view data (Collection View) reloads to incorrect Section after Callback

I have a collection view nested inside of a table view that is reloading the data to the wrong section after I make a call back. I'm not sure why this is happening so any help would be appreciated. I have it broken down into three files: TableViewController, CollectionViewCell and TableViewCell. Table view cell makes the call back to the table view controller in order to load and display ads; after this when the reload on the view controller is made it's reloading the data to the wrong sections:
TableViewController
import UIKit
import GoogleMobileAds
var data = [ThemesData(sectionType: "General", Themes: ["Themes/General/T001AAAK.png"]),
ThemesData(sectionType: "Plain", Themes: [
"Themes/Plain/T001AAAB.png", "Themes/Plain/T002AAAA.png", "Themes/Plain/T003AAAH.png", "Themes/Plain/T004AAAC.png", "Themes/Plain/T006AAAD.png", "Themes/Plain/T008AAAE.png", "Themes/Plain/T010AAAF.png", "Themes/Plain/T012AAAG.png", "Themes/Plain/T014AAAI.png", "Themes/Plain/T016AAAJ.png"
])
]
class ThemesViewController: UIViewController, GADFullScreenContentDelegate {
//Outlets
#IBOutlet weak var tableView: UITableView!
//Constants
let defaults = UserDefaults.standard
// Variables
var myTheme: String = ""
private var rewardedInterstitialAd: GADRewardedInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationItem.hidesBackButton = false
self.navigationController?.setNavigationBarHidden(false, animated: true)
GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866",
request: GADRequest()) { [self] ad, error in
if let error = error {
return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
}
self.rewardedInterstitialAd = ad
rewardedInterstitialAd?.fullScreenContentDelegate = self
}
}
func showAd() {
guard let rewardedInterstitialAd = rewardedInterstitialAd else {
return print("Ad wasn't ready.")
}
rewardedInterstitialAd.present(fromRootViewController: self) { [self] in
let reward = rewardedInterstitialAd.adReward
// Set new Theme
defaults.set(myTheme, forKey: "mTheme")
print(myTheme)
// set new Font
var tFont = myTheme.suffix(8)
tFont = tFont.prefix(4)
print(tFont)
defaults.set(tFont, forKey: "mFont")
// reload the themes view controller
}
}
/// Tells the delegate that the ad dismissed full screen content.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print(data)
tableView.reloadData()
GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866",
request: GADRequest()) { [self] ad, error in
if let error = error {
return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
}
self.rewardedInterstitialAd = ad
rewardedInterstitialAd?.fullScreenContentDelegate = self
}
}
}
extension ThemesViewController: UITableViewDelegate, UITableViewDataSource {
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
public func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return data[section].sectionType
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ThemeTableViewCell
cell.collectionView.tag = indexPath.section
guard let productsCell = cell as? ThemeTableViewCell else {
return cell
}
productsCell.didSelectItemAction = { [weak self] String in
self!.myTheme = String
self!.adAlert()
}
return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
}
public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = .colorFromHex("a5d7fa")
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.black
}
func adAlert() {
let showAlert = UIAlertController(title: "Unlock Themes", message: nil, preferredStyle: .alert)
let image = UIImage(systemName: "paintbrush")
showAlert.addImage(image: image!)
showAlert.addAction(UIAlertAction(title: "Watch Video", style: .default, handler: { action in
// Show Ad & update
self.showAd()
}))
showAlert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { action in
}))
self.present(showAlert, animated: true, completion: nil)
}
func setFont() {
// set new Font
var tFont = myTheme.suffix(8)
tFont = tFont.prefix(4)
print(tFont)
defaults.set(tFont, forKey: "mFont")
var mySubstring = myTheme.suffix(11)
}
}
CollectionViewCell
import UIKit
class ThemeCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var images: UIImageView!
#IBOutlet weak var padlock: UIImageView!
}
TableViewCell
import Foundation
import UIKit
import GoogleMobileAds
class ThemeTableViewCell: UITableViewCell {
//Outlets
#IBOutlet weak var collectionView: UICollectionView!
//Constants
let defaults = UserDefaults.standard
//Variables
var myTheme = ""
private var rewardedInterstitialAd: GADRewardedInterstitialAd?
var didSelectItemAction: ((String) -> Void)?
var retVal: String = ""
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
collectionView.delegate = self
collectionView.dataSource = self
myTheme = defaults.string(forKey: "mTheme")!
}
}
extension ThemeTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data[collectionView.tag].Themes.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let tCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ThemeCollectionViewCell
// check to see if this image requires a rewarded ad to unlock
if tCell.padlock.isHidden == false {
// we need a rewarded vide
print(data[collectionView.tag].Themes[indexPath.item])
let replaced = data[collectionView.tag].Themes[indexPath.item].replacingOccurrences(of: "T0", with: "I0")
didSelectItemAction?(replaced)
collectionView.reloadData()
} else {
// set new theme
print(data[collectionView.tag].Themes[indexPath.item])
let replaced = data[collectionView.tag].Themes[indexPath.item].replacingOccurrences(of: "T0", with: "I0")
defaults.set(replaced, forKey: "mTheme")
myTheme = defaults.string(forKey: "mTheme")!
// set new Font
var tFont = myTheme.suffix(8)
tFont = tFont.prefix(4)
print(tFont)
defaults.set(tFont, forKey: "mFont")
var mySubstring = myTheme.suffix(11)
collectionView.reloadData()
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ThemeCollectionViewCell
cell.images.image = UIImage(named: data[collectionView.tag].Themes[indexPath.row])
print(data[collectionView.tag].Themes[indexPath.item])
print(data[collectionView.tag].sectionType)
let myData = data[collectionView.tag].Themes[indexPath.item]
if data[collectionView.tag].sectionType == "General" {
cell.padlock.isHidden = true
} else {
cell.padlock.isHidden = false
}
let replaced = myData.replacingOccurrences(of: "T0", with: "I0")
if replaced == myTheme {
cell.images?.layer.borderWidth = 4.5
cell.images?.layer.borderColor = UIColor.red.cgColor
cell.images?.clipsToBounds = true
cell.padlock.isHidden = true
} else {
cell.images?.layer.borderWidth = 0.0
cell.images?.layer.borderColor = UIColor.white.cgColor
cell.images?.clipsToBounds = true
cell.padlock.isHidden = false
}
if data[collectionView.tag].sectionType == "General" {
cell.padlock.isHidden = true
} else {
if cell.images?.layer.borderColor == UIColor.red.cgColor {
cell.padlock.isHidden = true
} else {
cell.padlock.isHidden = false
}
}
return cell
}
}

Table View is now shown in simülatör swift/xcode

hello i am new to learning swift. I want to save data in database and show it in tableview. But even though I do everything, the tableview does not appear in the simulator. white screen appears. Any idea about the reason? I looked at the previous questions but couldn't find an answer.
import UIKit
import CoreData
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
#IBOutlet weak var tableView: UITableView!
//tanımlamalar
var nameArray = [String]()
var idArray = [UUID]()
override func viewDidLoad() {
super.viewDidLoad()
//tableview gösterme
tableView.delegate = self
tableView.dataSource = self
//add butonu ekleme
navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(addButtonClicked))
}
//verileri getirme
func getData() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
//getirme
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName : "Paintings")
fetchRequest.returnsObjectsAsFaults = false
do{
let results = try context.fetch(fetchRequest)
for result in results as! [NSManagedObject] {
if let name = result.value(forKey: "name") as? String{
self.nameArray.append(name)
}
if let id = result.value(forKey: "id") as? UUID{
self.idArray.append(id)
}
//yeni veri eklenirse tableviewı reload et
self.tableView.reloadData()
}
}catch{
print("error")
}
}
//add butonuna tıklanırsa ne olacak
#objc func addButtonClicked() {
performSegue(withIdentifier: "DetailsVC", sender: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
var content = cell.defaultContentConfiguration()
content.text = nameArray[indexPath.row]
cell.contentConfiguration = content
return cell
}
}

How I can pin the tableHeaderView?

Tell me, please, how I can pin the tableHeaderView on the ViewController's screen through code? I use this code, but the tableViewHeader disappears on scrolling:
import UIKit
class TestViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
lazy var tableViewTest = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
createTable()
}
private func createTable() {
self.tableViewTest = UITableView(frame: view.bounds, style: .grouped)
tableViewTest.register(TestTableViewCell.self, forCellReuseIdentifier: "Test")
self.tableViewTest.delegate = self
self.tableViewTest.dataSource = self
tableViewTest.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableViewTest.separatorInset.left = 10
tableViewTest.separatorInset.right = 10
tableViewTest.tableHeaderView = "Test Header"
view.addSubview(tableViewTest)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Test", for: indexPath) as! TestTableViewCell
cell.testLabel.text = "test label"
return cell
}
}
This is my second class:
import UIKit
class TestTableViewCell: UITableViewCell {
let testLabel = UILabel()
override func layoutSubviews() {
super.layoutSubviews()
testLabel.frame = CGRect(x: 60, y: 5, width: UIScreen.main.bounds.width - 80, height: 50)
testLabel.numberOfLines = 0
testLabel.sizeToFit()
addSubview(testLabel)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Try to use the implicit construct of UITableView
lazy var tableViewTest = UITableView(frame: .zero, style: .plain)
But also you need to change your table header to section header which you can define in UITableViewDelegate method
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let identifier = YourSectionHeaderView.reuseIdentifier
let headerView =
tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
return headerView
}
By the way, I also recommend you to use constraint instead of autoresizing mask
Thank you.
This work for me:
import UIKit
class TestViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
lazy var tableViewTest = UITableView()
var segmentedControl = UISegmentedControl(items: ["Test1", "Test2", "Test3"])
override func viewDidLoad() {
super.viewDidLoad()
createTable()
configureSegmentedControl()
}
private func createTable() {
self.tableViewTest = UITableView(frame: view.bounds, style: .plain)
tableViewTest.register(TestTableViewCell.self, forCellReuseIdentifier: "Test")
self.tableViewTest.delegate = self
self.tableViewTest.dataSource = self
tableViewTest.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableViewTest.separatorInset.left = 10
tableViewTest.separatorInset.right = 10
tableViewTest.tableFooterView = UIView()
view.addSubview(tableViewTest)
}
private func configureSegmentedControl() {
segmentedControl.selectedSegmentIndex = 0
segmentedControl.frame = CGRect(x: 10, y: 150, width: UIScreen.main.bounds.width - 20.0, height: 40)
segmentedControl.layer.cornerRadius = 5.0
segmentedControl.backgroundColor = .blue
segmentedControl.selectedSegmentTintColor = .red
segmentedControl.tintColor = .white
segmentedControl.addTarget(self, action: #selector(changeSegment), for: .valueChanged)
}
#objc func changeSegment(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
print("Test1")
case 1:
print("Test2")
default:
print("Test3")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Test", for: indexPath) as! TestTableViewCell
cell.testLabel.text = "test label"
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return segmentedControl
}
}

Filter data from TableView

How can I filter a tableView by search bar? I fetch data to the tableView from a Firebase database and my search bar is inside navigationBar.
This is my code:
class TableViewCustomer: UIViewController, UITableViewDelegate, UITableViewDataSource {
var ref:DatabaseReference!
var customerList = [CustomerModel]()
lazy var searchBar: UISearchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 300, height: 20))
#IBOutlet weak var tableViewCustomer: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.placeholder = "Search"
navigationItem.titleView = searchBar
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.gray
if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 18
backgroundview.clipsToBounds = true
}
}
tableViewCustomer.delegate = self
tableViewCustomer.dataSource = self
ref = Database.database().reference().child("Customer");
ref.observe(DataEventType.value, with: { (snapshot) in
if snapshot.childrenCount > 0 {
self.customerList.removeAll()
for results in snapshot.children.allObjects as! [DataSnapshot] {
let results = results.value as? [String: AnyObject]
let name = results?["Name and surname"]
let phone = results?["Phone"]
let company = results?["Company name"]
let myCustomer = CustomerModel(name: name as? String, phone: phone as? String, company: company as? String)
self.customerList.append(myCustomer)
}
self.tableViewCustomer.reloadData()
}
})
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
#objc func buttonAction(sender: UIButton!) {
dismiss(animated: true, completion: nil)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return customerList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewCustomer.dequeueReusableCell(withIdentifier: "cellCustomer") as! TableViewCellCustomer
cell.layer.backgroundColor = UIColor.init(white: 1, alpha: 0.7).cgColor
let customer: CustomerModel
customer = customerList[indexPath.row]
cell.nameLabel.text = customer.name
cell.phoneLabel.text = customer.phone
cell.companyLabel.text = customer.company
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "description", sender: self)
}
}
Add your search bar, and an empty array. As you search add the suitable items to the empty array, then remove them from the array. Use the new array to index into your UI.

Swift - Tableview Cells Stay Highlighted - Except for Top One - No Matter What

So I'm new to swift and I've been having some problems getting all of my tableview cells to deselect after performing the segue. Right now all of the cells stay highlighted after I perform the segue, except the top one. The top one is selectable too and performs the segue, but it has no highlighting at all.
This is strange behavior but I have tried all of the basics.
I've tried
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
I've tried
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
self.tableView.deselectRow(at: selectionIndexPath, animated: animated)
}
}
I've also tried it in the prepare for segue method.
I've also tried it from the cell: cell.selectionStyle = .none
I've also tried changing "selection" in the story board to none.
Nothing seems to change the behavior so I'm at a loss. I think I've messed up something somewhere and can't find what it is.
Here is my tableview class in its entirety if anyone wants to take a look.
import UIKit
import Firebase
class DraftListCell : UITableViewCell {
#IBOutlet weak var playerName: UILabel!
#IBOutlet weak var playerPrice: UILabel!
#IBOutlet weak var priceRemaining: UILabel!
#IBOutlet weak var vsLabel: UILabel!
#IBOutlet weak var injuredLabel: UILabel!
#IBOutlet weak var gameTimeLabel: UILabel!
}
class NBADraftList: UIViewController, UITableViewDelegate, UITableViewDataSource, FIRDatabaseReferenceable{
#IBOutlet var tableView: UITableView!
let cellReuseIdentifier = "cell"
var ref: FIRDatabaseReference!
var players = [Player]()
let formatter = NumberFormatter()
override func viewDidLoad() {
let leftBarButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(myLeftSideBarButtonItemTapped(_:)))
self.title = "Select"
self.navigationItem.leftBarButtonItem = leftBarButton
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
self.tableView.rowHeight = 100.0
self.tableView.tableFooterView = UIView()
tableView.delegate = self
tableView.dataSource = self
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 0
ref = FIRDatabase.database().reference().child("NBATodaysPlayers")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
var players = [Player]()
for player in snapshot.children {
players.append(Player(snapshot: player as! FIRDataSnapshot))
}
self.players = players.sorted(by: { $0.Value > $1.Value })
self.tableView.reloadData()
}) { (error) in
print("error")
}
super.viewDidLoad()
}
func myLeftSideBarButtonItemTapped(_ sender:UIBarButtonItem!)
{
self.dismiss(animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return players.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DraftListCell", for: indexPath as IndexPath) as! DraftListCell
if let formattedPrice = formatter.string(from: players[indexPath.row].Value as NSNumber) {
cell.playerPrice.text = "Game Price: \(formattedPrice)"
}
if let formattedRemainingPrice = formatter.string(from: 10000 - players[indexPath.row].Value as NSNumber) {
cell.priceRemaining.text = "Remaining: \(formattedRemainingPrice)"
}
cell.playerName.text = players[indexPath.row].Name
cell.injuredLabel.text = players[indexPath.row].Inj
cell.vsLabel.text = players[indexPath.row].visiting + " # " + players[indexPath.row].home
cell.gameTimeLabel.text = players[indexPath.row].game_time
cell.textLabel?.textColor = .white
cell.backgroundColor = .black
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "BuyStats", sender: indexPath);
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "BuyStats" {
let buyStats = segue.destination as! BuyStats
if let indexPath = tableView.indexPathForSelectedRow {
let player = players[indexPath.row]
buyStats.selectedPlayer = player
}
}
}
}
In your cellForRowAt add following:
if cell.isSelected == true {
tableView.deselectRow(at: indexPath, animated: false)
}
Or if you want to do this specifically by Segue. You can set a global variable let's say deselectAll and check if it's true in cellForRowAt
Hope this helps