How to put Google Maps in TableView - Swift 3 - ios

I'm new to Swift Programming. I'm trying to put Google Map in UITableView. Can I do that?
The User Interface should be like this:
Here is my code for implementing UITableView using Storyboard:
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rowCount = 0
if section == 0 {
rowCount = 1
}
if section == 1 {
rowCount = arrayOfStatic.count
}
if section == 2 {
rowCount = arrayOfDynamic.count
}
return rowCount
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return 250
} else {
return 70
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "mapsCellId", for: indexPath) as! GoogleMapsCell
return cell
} else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "staticCellId", for: indexPath) as! StaticCell
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "placesCellId", for: indexPath) as! PlacesCell
let ip = indexPath
cell.imageFoursquarePlaces.image = arrayOfDynamic[indexPath.row].image
cell.labelPlacesName.text = arrayOfDynamic[ip.row].name
cell.labelPlacesCategory.text = arrayOfDynamic[ip.row].category
return cell
}
}
Here is my code for implementing UITableViewCell in the first section of UITableView:
class GoogleMapsCell: UITableViewCell, GMSMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet weak var googleMapsView: UIView!
#IBOutlet weak var buttonCurrentLoc: UIButton!
var googleMaps: GMSMapView!
var locationManager = CLLocationManager()
var camera = GMSCameraPosition()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locationManager.stopUpdatingLocation()
}
func showCurrentLocationOnMap() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
let camera = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!, zoom: 15)
self.googleMaps = GMSMapView.map(withFrame: CGRect(x: 0,y: 0, width: self.googleMapsView.frame.size.width, height: self.googleMapsView.frame.height), camera: camera)
do {
if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
self.googleMaps.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
} else {
NSLog("Unable to find style.json")
}
} catch {
NSLog("The style definition could not be loaded: \(error)")
}
self.googleMaps.isMyLocationEnabled = true
self.googleMaps.accessibilityElementsHidden = false
self.addSubview(self.googleMaps)
self.googleMaps.camera = camera
self.addSubview(self.buttonCurrentLoc)
}
}
It gives me result:
I have a searchBar as HeaderView. I tried to put Google Map View by creating a UIView inside the first cell of TableView. But I can't show the Google Map View and the button can't be clicked. How can I do that?
Any help would be appreciated :)

You forgot to call showCurrentLocationOnMap method with your GoogleMapsCell's instance in cellForRowAtIndexPath.
let cell = tableView.dequeueReusableCell(withIdentifier: "mapsCellId", for: indexPath) as! GoogleMapsCell
//add this method call
cell.showCurrentLocationOnMap()
return cell
Or you can override awakeFromNib in your GoogleMapsCell and call the showCurrentLocationOnMap method with it.
override func awakeFromNib() {
super.awakeFromNib()
self.showCurrentLocationOnMap()
}

Related

Not able to select tableview's cell

I'm using Xcode 11.2 and I have a view containing 3 tableviews. They all have outlets to the code and their delegates and datasources are correctly set in the viewDidLoad method.
What happens is that the cell highlights when clicked but then it doesn't remain selected (didSelectRow never gets called).
If I click many times rapidly with the left and right mouse buttons the cell will eventually get selected and the didSelectRow gets called.
Also I noticed that the selection almost always works if I click the cell and at the same time scroll the table a little.
Here some of the code:
#IBOutlet weak var linesTableView: UITableView!
#IBOutlet weak var firstLvlTableView: UITableView!
#IBOutlet weak var secondLvlTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.linesTableView.dataSource = self
self.linesTableView.delegate = self
self.firstLvlTableView.delegate = self
self.firstLvlTableView.dataSource = self
self.secondLvlTableView.dataSource = self
self.secondLvlTableView.delegate = self
if (CLLocationManager.locationServicesEnabled()) {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
self.secondLvlTableView.isHidden = true
self.firstLvlTableView.isHidden = false
self.linesTableView.isHidden = true
fetchData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == linesTableView {
return 0
} else if tableView == firstLvlTableView {
return self.salesAssistantLocalized.count //viewModel.typesOfSale.lists.endIndex
} else if tableView == secondLvlTableView {
return 4 //viewModel.standardTableLevels.lists.endIndex
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == firstLvlTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstLvlCell", for: indexPath)
cell.textLabel?.text = self.salesAssistantLocalized[indexPath.row]
return cell
} else if tableView == secondLvlTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "SecondLvlCell", for: indexPath)
cell.textLabel?.text = self.levels[indexPath.row]
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "SaleTypeCell", for: indexPath)
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
printDebug("Did Select Row \(indexPath.row) for table \(tableView)")
if tableView == firstLvlTableView {
if indexPath.row == 0 {
self.secondLvlTableView.isHidden = false
}
}
}
I've found out what is happening.
All the view controllers are extending from a "BaseViewController" that has a tap gesture recognizer for dismissing the keyboard.
That is causing the problem.
I've simply added a Bool to be able to choose if use it or not:
open var useTapGesture: Bool = true
and then wrapped the tap gesture code with this:
if useTapGesture {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(BaseViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
This way I can use
useTapGesture = false
in my viewDidLoad method.

How to show a parent view which is outside tableview and is scrollable?

I have a scenario where I need to show a parent view with shadow and corner radius containing a long list of reusable items. I used a tableView to display items. But I stuck at making my tableview expand as much as its contentSize. It works but not accurate. Any solutions?
Edit:
Desired result:
I used the following reference for self sizing tableview.
Self Sizing UITableView
I made a few modifications as below:
final class SelfSizedTableView: UITableView {
var maxHeight = CGFloat.greatestFiniteMagnitude
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
self.layoutIfNeeded()
}
override var intrinsicContentSize: CGSize {
let height = min(contentSize.height, maxHeight)
let size = CGSize(width: contentSize.width, height: height)
return size
}
}
I used a parent tableView with a cell having my containerView and embedding this self sized tableView.
class MyContainerViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// MARK: - IBOutlets
#IBOutlet weak var parentTableView: UITableView!
// MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
private func estimateDataHeight() -> CGFloat {
let detailCellHeight: CGFloat = 32
let headingCellHeight: CGFloat = 43
let headings: CGFloat = headingCellHeight*2
let detailsHeight: CGFloat = detailCellHeight*4
let baseHeight = headings + detailsHeight
let membersHeight =
CGFloat(sectionsArray.count) * detailCellHeight
return baseHeight + membersHeight
}
}
// MARK: - UITableViewDataSource
extension MyContainerViewController {
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let id = String(describing: MyContainerTVCell.self)
guard let cell = tableView
.dequeueReusableCell(withIdentifier: id, for: indexPath)
as? MyContainerTVCell else {
return UITableViewCell()
}
cell.policyDetails = dataSource
// my cheat/trick doesn't work on large data.
DispatchQueue.main.asyncAfter(deadline: .now()+0.4) {
tableView.beginUpdates()
cell.tableView.layoutIfNeeded()
cell.tableView.reloadData() // the overridden one
tableView.endUpdates()
}
return cell
}
}
extension MyContainerViewController {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return estimateDataHeight()
}
}
My cell class which has the self size tableView and containerView:
class MyContainerTVCell: UITableViewCell, UITableViewDataSource, UITableViewDelegate {
// MARK: - IBOutlets
#IBOutlet weak var containerView: UIView!
#IBOutlet weak var shadowView: UIView!
#IBOutlet weak var tableView: SelfSizedTableView!
// MARK: - Properties
let titles = ["Email ID:", "Mobile Number:", "Address:", "ID: "] // first section data array
let moreData: [String] = [] // remaining reusable sections array
// no of subsequent sections for moreData array type
var numberOfSections: Int {
return 4
}
// MARK: -
var dataSource: MyDataSource!
// MARK: - Life Cycle
override func awakeFromNib() {
super.awakeFromNib()
setupView()
}
override func layoutSubviews() {
super.layoutSubviews()
}
// MARK: - Setup
func setupView() {
containerView.rounded(with: 10)
shadowView.layer.applyShadow()
tableView.dataSource = self
tableView.delegate = self
}
}
// MARK: - UITableViewDataSource
extension MyContainerTVCell {
func numberOfSections(in tableView: UITableView) -> Int {
return numberOfSections + 1
}
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
if section == 0 { return titles.count + 1 }
else if section == 1 { return moreData.count + 1 }
else { return moreData.count }
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let headerID = String(describing: MyHeaderTVCell.self)
let itemID = String(describing: MyItemTVCell.self)
switch indexPath.section {
case 0:
if indexPath.row == 0 {
guard let cell = tableView
.dequeueReusableCell(withIdentifier: headerID, for: indexPath)
as? MyHeaderTVCell else {
return UITableViewCell()
}
cell.titleLabel.text = dataSource.title
return cell
} else {
guard let cell = tableView
.dequeueReusableCell(withIdentifier: itemID, for: indexPath)
as? MyItemTVCell else {
return UITableViewCell()
}
let item = titles[indexPath.row-1]
cell.titleLabel.text = item
cell.separatorView.isHidden = true
let data: String
switch indexPath.row {
case 1:
data = dataSource.emailID
case 2:
data = dataSource.mobileNo
case 3:
data = dataSource.address
case 4:
data = dataSource.name
case 5:
data = dataSource.age
case 6:
data = dataSource.id
case 7:
data = dataSource.office
case 8:
data = dataSource.academic
default: data = String()
}
cell.detailLabel.text = data
return cell
}
case 1:
if indexPath.row == 0 {
guard let cell = tableView
.dequeueReusableCell(withIdentifier: headerID, for: indexPath)
as? MyHeaderTVCell else {
return UITableViewCell()
}
cell.titleLabel.text = "More Data"
return cell
} else {
guard let cell = tableView
.dequeueReusableCell(withIdentifier: itemID, for: indexPath)
as? MyItemTVCell else {
return UITableViewCell()
}
let sectionIndex = indexPath.section-1
guard sectionIndex <= numberOfSections-1,
let section = sectionsArray?[indexPath.section-1] else {
return UITableViewCell()
}
cell.titleLabel.text = moreData[indexPath.row-1]
cell.separatorView.isHidden = true
switch indexPath.row {
case 1:
cell.detailLabel.text = section.a
case 2:
cell.detailLabel.text = section.b
case 3:
cell.detailLabel.text = "\(section.c ?? 0)"
case 4:
cell.detailLabel.text = section.d
case 5:
cell.detailLabel.text = section.e
case 6:
cell.detailLabel.text = section.f
if indexPath.section < numberOfSections {
cell.separatorView.isHidden = false
}
default: break
}
return cell
}
default:
guard let cell = tableView
.dequeueReusableCell(withIdentifier: itemID, for: indexPath)
as? MyItemTVCell else {
return UITableViewCell()
}
let sectionIndex = indexPath.section-1
guard sectionIndex <= numberOfSections-1,
let section = sectionsArray?[indexPath.section-1] else {
return UITableViewCell()
}
cell.titleLabel.text = moreData[indexPath.row]
cell.separatorView.isHidden = true
switch indexPath.row {
case 0:
cell.detailLabel.text = section.a
case 1:
cell.detailLabel.text = section.b
case 2:
cell.detailLabel.text = "\(section.c ?? 0)"
case 3:
cell.detailLabel.text = section.d
case 4:
cell.detailLabel.text = section.e
case 5:
cell.detailLabel.text = section.f
if indexPath.section < numberOfSections {
cell.separatorView.isHidden = false
}
default: break
}
return cell
}
}
}
// MARK: - UITableViewDelegate
extension MyContainerTVCell {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 && indexPath.row == 0 { return 43 }
if indexPath.section == 1 && indexPath.row == 0 { return 43 }
return 32
}
}
Why would you want to expand tableView as much as its content size to make it scrollable, when tableView is already scrollable?
However, if you have some other content, aside from table, on the screen and you want them to scroll together, then you need to embed all your content into UIScrollView.
Then, make a height constraint for you tableView in xib/storyboard with any value.
Then you might do something like this:
// in your view controller
private var heightObservation: NSKeyValueObservation?
// called once, for example, in viewDidLoad()
private func setupTableView() {
...
observation = tableView.constraintFrameHeightToContentSizeHeight()
}
extension UITableView {
func constraintFrameHeightToContentSizeHeight() -> NSKeyValueObservation {
return observe(\.contentSize, changeHandler: { (tableView, _) in
tableView.heightConstraint?.constant = tableView.contentSize.height
})
}
}
// find height constraint
extension UIView {
var heightConstraint: NSLayoutConstraint? {
return constraints.first(where: { $0.firstAttribute == .height })
}
}
Don't forget to uncheck "Scrolling Enabled" in xib/storyboard for that table view.

Hide Selected Cell from the Table - Swift4

I have a list with 4 objects of places that I query from my Realm database.
Optional(Results<Place> <0x7feaaea447c0> (
[0] Place {
name = Daniel Webster Highway;
country = United States;
lat = 42.72073329999999;
lon = -71.44301460000001;
},
[1] Place {
name = District Avenue;
country = United States;
lat = 42.48354969999999;
lon = -71.2102486;
},
[2] Place {
name = Gorham Street;
country = United States;
lat = 42.62137479999999;
lon = -71.30538779999999;
},
[3] Place {
name = Route de HHF;
country = Haiti;
lat = 18.6401311;
lon = -74.1203939;
}
))
I'm trying to hide the selected one.
Ex. When I click on Daniel Webster Highway, I don't want that to show on my list.
How would one go above and do that in Swift 4 ?
Code
//
// PlaceDetailVC.swift
// Memorable Places
//
//
import UIKit
import CoreLocation
import RealmSwift
class PlaceDetailVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var address: UILabel!
#IBOutlet weak var placesTable: UITableView!
var selectedPlace : Place = Place()
var selectedTrip : Trip = Trip()
var distances = [ String ]()
var places : Results<Place>?
override func viewDidLoad() {
super.viewDidLoad()
address.text = selectedPlace.name
//register xib file
placesTable.register(UINib(nibName: "PlaceDetailCell", bundle: nil), forCellReuseIdentifier: "customPlaceDetailCell")
}
override func viewDidAppear(_ animated: Bool) {
load()
if selectedPlace != nil && places != nil {
for i in 0..<places!.count {
let latitude = Double(places![i].lat)
let longitude = Double(places![i].lon)
let currentLatitude = Double(selectedPlace.lat)
let currentLongitude = Double(selectedPlace.lon)
//print(latitude,longitude,currentLatitude,currentLongitude)
let coordinate = CLLocation(latitude: latitude, longitude: longitude)
let currentCoordinate = CLLocation(latitude: currentLatitude, longitude: currentLongitude)
let distanceInMeters = coordinate.distance(from: currentCoordinate) // result is in meters
let distanceInMiles = distanceInMeters/1609.344
distances.append(String(format: "%.2f", distanceInMiles))
}
}
}
// ---------------------------------------------------------------------------------------------------------
//MARK - CRUD functions
//Read
func load() {
places = selectedTrip.places.sorted(byKeyPath: "name", ascending: true)
//print(places,"<<<")
placesTable.reloadData()
}
// ---------------------------------------------------------------------------------------------------------
//MARK - Table View Datasource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return places?.count ?? 0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customPlaceDetailCell", for: indexPath)
as! CustomPlaceDetailCell
if selectedPlace.name != nil {
cell.address.text = (places![indexPath.row]["name"] as! String)
cell.distance.text = distances[indexPath.row]
}
return cell
}
// ---------------------------------------------------------------------------------------------------------
//MARK - Table View Delegate
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
activePlace = indexPath.row
}
}
var distances = [ String ]()
var places : Results<Place>?
Then in tableView(_:cellForRow:)
cell.address.text = (places![indexPath.row]["name"] as! String)
cell.distance.text = distances[indexPath.row]
Just don't do that. These info need to be synchronized.
Instead, use another class/struct, or an extension which will hold the distance and the place.
var array: [PlaceModel]
struct PlaceModel {
let place: Place
let distance: Double //You can use String, but that's bad habit
//Might want to add the "image link" also?
}
In load():
array.removeAll()
let tempPlaces = selectedTrip.places.sorted(byKeyPath: "name", ascending: true)
for aPlace in tempPlaces {
let distance = //Calculate distance for aPlace
array.append(PlaceModel(place: aPlace, distance: distance)
}
Now, in tableView(_:cellForRow:):
let aPlaceModel = array[indexPath.row]
if activePlace == indexPath {
let cell = tableView.dequeue...
//Use cellWithImage for that place
return cell
} else {
let cell = tableView.dequeue...
cell.address.text = aPlaceModel.place.name
cell.distance.text = aPlaceModel.distance
return cell
}
And keep that logic wherever you want, if the heightForRow if needed (if you want for instance that all your images be at 80pt but the rest at 44pts, etc.
In tableView(_:didSelectRowAt:), add tableView.reloadData(), or better tableView.reloadRows(at: [indexPath] with: .automatic)
NB: Code not tested, might not compile, but you should get the idea.
You could pass the index of the selected row from placesVC to your PlaceDetailVC and in
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == passedIndex {
return 0
}
return 70
}
set the cell height to 0 to hide the cell.

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

Updating Tableview Cells with a Button in swift

Hello everybody i was wonder if you can update the cells inside of a button? Ive have the code where the button is pressed and it gets your lat/lng and print them out, but now i'm trying to update the Lat and Lng cells in my other swift file CoordinatesAltitudeDegreesTableViewCell.swift
as of now my tableview functions seem to work and i tested updating the cell in the function in the viewController and it works.
My question is anyway to put those functions in the button so the lat/lng variables are in scope
Sidenote: ive been looking for solutions for two days and i could seem to find any page that helped me, hopefully this isnt a repost and if there is then sorry in advance
*again, sorry i am very new to swift programming
import UIKit
import Alamofire
import SwiftyJSON
import CoreLocation
import MapKit
class myTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate{
//----------------------
// Buttons and Variables
//----------------------
var lastLocation: CLLocation? = nil
#IBOutlet weak var tableView: UITableView!
#IBAction func generateInfo(sender: AnyObject) {
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
let lat: Double! = lastLocation?.coordinate.latitude
let lng: Double! = lastLocation?.coordinate.longitude
let alt: Double! = lastLocation?.altitude
print(lat)
print(lng)
print(alt)
locationManager.stopUpdatingLocation()
} // end of button
//--------------
// Main Function
//--------------
override func viewDidLoad(){
super.viewDidLoad()
print("Hello World")
self.tableView.dataSource = self
self.tableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//-------------------
// LOCATION FUNCTIONS
//-------------------
// Represents Location Manager
lazy var locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
return manager
}()
// Location Authorization Function
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus){
if case .authorizedWhenInUse = status {
manager.requestLocation()
} else {
print("yeah... that didn't work")
}
}
// Location Error handle
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("that didn't work")
}
// Location Object
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
if let location = locations.first {
lastLocation = location
}
}
//--------------------
// TABLEVIEW FUNCTIONS
//--------------------
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCellCustomCell", for: indexPath) as! ImageCellTableViewCell
return cell
}
else if indexPath.row == 1{
let cell = self.tableView.dequeueReusableCell(withIdentifier: "coordinatesAltitudeDegreesCustomCell", for: indexPath) as! CoordinatesAltitudeDegreesTableViewCell
cell.latLabel.text = "text"
return cell
}
else if indexPath.row == 2{
let cell = tableView.dequeueReusableCell(withIdentifier: "censusGeographyCustomCell", for: indexPath) as! CensusGeographyTableViewCell
return cell
}
else if indexPath.row == 3{
let cell = tableView.dequeueReusableCell(withIdentifier: "nearestAddressCustomCell", for: indexPath) as! NearestAddressTableViewCell
return cell
}
else if indexPath.row == 4{
let cell = tableView.dequeueReusableCell(withIdentifier: "populationCustomCell", for: indexPath) as! PopulationTableViewCell
return cell
}
else if indexPath.row == 5{
let cell = tableView.dequeueReusableCell(withIdentifier: "ethnicityCustomCell", for: indexPath) as! EthnicityTableViewCell
return cell
}
else if indexPath.row == 6{
let cell = tableView.dequeueReusableCell(withIdentifier: "raceCustomCell", for: indexPath) as! RaceTableViewCell
return cell
}
else if indexPath.row == 7{
let cell = tableView.dequeueReusableCell(withIdentifier: "ageCustomCell", for: indexPath) as! AgeTableViewCell
return cell
}
else if indexPath.row == 8{
let cell = tableView.dequeueReusableCell(withIdentifier: "populationPyramidCustomCell", for: indexPath) as! PopulationPyramidTableViewCell
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "houseHoldCustomCell", for: indexPath) as! HouseHoldTableViewCell
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 300
}
else if indexPath.row == 1{
return 130
}
else if indexPath.row == 2{
return 100
}
else if indexPath.row == 3{
return 100
}
else if indexPath.row == 4{
return 90
}
else if indexPath.row == 5{
return 110
}
else if indexPath.row == 6{
return 200
}
else if indexPath.row == 7{
return 300
}
else if indexPath.row == 8{
return 300
}
else {
return 360
}
}
}
You should have variables inside your class for lat, long, alt and in your CoordinatesAltitudeDegreesTableViewCell you also should have those variables.
So for example here's a code snippet.
class SomeViewController: UIViewController {
// Set these attributes here cause they will be used as your source of data for the UITableViewCell
var lat: Int = 0
var lon: Int = 0
var alt: Int = 0
#IBOutlet var tableView: UITableView!
#IBOutlet var someButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
someButton.addTarget(self, action: #selector(self.someButtonClicked(_:)), forControlEvents: .TouchUpInside)
}
func someButtonClicked(sender: UIButton) {
lat = 100 // set to different values
lon = 10 // set to different values
alt = 30 // set to different values
// when you reload the tableView all the `UITableViewDelegate, UITableViewDataSource` codes will be called again therefore refreshing your cells.
tableView.reloadData()
}
}
extension SomeViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SomeCustomCell", for: indexPath) as! SomeCustomCell
// set the parameters
cell.lat = self.lat
cell.lon = self.lon
cell.alt = self.atl
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
}
class SomeCustomCell: UITableViewCell {
var lat: Int = 0 {
didSet { // just in case you don't know about didSet there's an explanation below
lblLat.text = "\(lat)"
}
}
var lon: Int = 0 {
didSet {
lblLon.text = "\(lon)"
}
}
var alt: Int = 0 {
didSet {
lblAlt.text = "\(alt)"
}
}
... insert some ui objects here
#IBOutlet var lblLat: UILabel!
#IBOutlet var lblLon: UILabel!
#IBOutlet var lblAlt: UILabel!
... insert some ui objects here
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
There are a multitude of ways to do this, but this is the simplest one I can think of.
didSet explanation
var someVariable: <SomeDataType> = <SomeValue> {
didSet {
// this block is called whenever this variable's value is changed (i.e someVariable)
}
}
for example:
var name: String = "" {
didSet {
print(name)
}
}
init() {
name = "Capaldi"
name = "Tennant"
name = "Smith"
name = "Eccleston"
}
In your debugger it would output something like this since didSet was called each time you set the name
Capaldi
Tennant
Smith
Eccleston

Resources