Calling a function in another class Swift - ios

I need to call this function. I need to receive the user identityString. How can i go about doing this?
class GeneralChatroom: UIViewController,
UITableViewDataSource,
UITableViewDelegate,
UITextFieldDelegate,
UITextViewDelegate {
//Get Data of current cell that has been tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let userIdentityString = generalRoomDataArr[indexPath.row].cellUserId
print("Uid of cell Data: " + userIdentityString!)
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
//Transform Data From ^ to load at the bottom
tableView.transform = CGAffineTransform (scaleX: 1,y: -1);
cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1);
cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1);
//Set username label to display username
let usernameLabel = cell?.viewWithTag(1) as! UILabel
usernameLabel.text = generalRoomDataArr[indexPath.row].username
//Set mesage TextView Label to display message in textView
let messageLabel = cell?.viewWithTag(5) as! UITextView
messageLabel.text = generalRoomDataArr[indexPath.row].message
//TO DO: dont know if this actually works prob can delete
messageLabel.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
//initialize UI Profile Image
let imageView = cell?.viewWithTag(3) as! UIImageView
//Make Porfile Image Cirlce
imageView.layer.cornerRadius = imageView.frame.size.width/2
imageView.clipsToBounds = true
//Set timeStampLabel to current time AGO
let timeStampLabel = cell?.viewWithTag(4) as! UILabel
timeStampLabel.text = generalRoomDataArr[indexPath.row].timeStamp
timeStampLabel.numberOfLines = 0
//Loading and change of Usesrs profile image on chat cell
let userProfileChatImage = generalRoomDataArr[indexPath.row].photoURL
//Load profile image(on cell) with URL & Alamofire Library
let downloadURL = NSURL(string: userProfileChatImage!)
imageView.af_setImage(withURL: downloadURL as! URL)
return cell!
}
}
But I need to call that function in a different class to get the userIdentityString. How can I do this? I need to call it in the Image Tapped function.
class GeneralChatroomTableViewCell: UITableViewCell {
#IBOutlet weak var profileImageView: UIImageView!
#IBOutlet weak var textViewHeight: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
// You must to use interaction enabled
profileImageView.isUserInteractionEnabled = true
profileImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:))))
}
func imageTapped(_ sender: UITapGestureRecognizer) {
//first you need to call the function that reads the cell. Recieve the UID
print("image tapped")
}
}
maybe this will be more clear
func imageTapped(_ sender: UITapGestureRecognizer) {
let userIDString = GeneralChatroom.tableView(userIdentityString: userIdentityString)
print(userIDString)
//first you need to call the function that reads the cell. Recieve the UID
print("image tapped")
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) -> String{
let userIdentityString = generalRoomDataArr[indexPath.row].cellUserId
print("Uid of cell Data: " + userIdentityString!)
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
//1.) If imageView touch is deteected
//2.) Segua to new view controller by passing in the string UID(userIdentityString) of the cell
//3.) Get database information based on the UID that is added(look at prevous methods)
// -might have to call this function and use separeate function
//4.) Output data from database Users to represent a user profile
//All you have to do is pass a UID (Check other Database methods to send UID)
return userIdentityString!
}

OK, as I stated in my comment this answer is not about calling another function from a swift class because you are trying to call a delegate function. I am not sure it can be guaranteed that function will have the expected value if called manually. What I would do is create a property on your TableViewCell to hold the userId.
class GeneralChatroomTableViewCell: UITableViewCell {
#IBOutlet weak var profileImageView: UIImageView!
#IBOutlet weak var textViewHeight: NSLayoutConstraint!
// User Id
var userId: String?
override func awakeFromNib() {
super.awakeFromNib()
// You must to use interaction enabled
profileImageView.isUserInteractionEnabled = true
profileImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:))))
}
func imageTapped(_ sender: UITapGestureRecognizer) {
//first you need to call the function that reads the cell. Recieve the UID
print("image tapped")
if let uid = self.userId {
// Do whatever you want with userId
}
}
}
So I added an optional userId property on the TableViewCell. In order to populate that we will do it in the func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { delegate function. I am not sure what yours looks like since you didn't provide that code so I am just going on what it generally looks like and what your GeneralChatroomTableViewCell is.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(GeneralChatroomTableViewCell.self), for: indexPath) as! GeneralChatroomTableViewCell
(cell as! GeneralChatroomTableViewCell).userId = generalRoomDataArr[indexPath.row].cellUserId
return cell
}
This function is called whenever a cell is being created for display in the table. So now on creation of the cell we set the userId so that its always available for us whenever we need it.
And as stated in the comment above this is only useful if that data is available at the time of creation of the cell.

Related

Get Switch State from specific cell

I am working on a iOS app that has two ViewControllers. The first is a TableView which creates a row for each index in a array. Each cell of this TableView shows the content in the array corresponding to the index and has a switch. The second ViewController has an image and a label and they are supposed to change depending the switch state. So, how can I get the switch state from a specific cell?
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var state_label: UILabel!
#IBOutlet weak var descr_label: UILabel!
#IBOutlet weak var myimg: UIImageView!
let arr: [String] = ["Text1","Text2", "Text3", "Text4"]
var switch_isOn = false
override func viewDidLoad() {
super.viewDidLoad()
if(switch_isOn == false){
myimg?.image = UIImage(named: "img1")
}else{
myimg?.image = UIImage(named: "img2")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = arr[indexPath.row]
let mySwitch = UISwitch()
cell.accessoryView = mySwitch
mySwitch.tag = 1001
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let segueIdentifier: String
segueIdentifier = "segue"
// Get the selected Cell and Iterate through it's subviews to find the switch using the tag
let cell = tableView.cellForRow(at: indexPath)
//Get the Cell Text
print("\(cell?.textLabel?.text ?? "")")
// Iterate through subviews of Cell
for v in cell?.subviews ?? [] {
// If a view found with tag == 1001 then it's the switch view because we had assigned 1001 to the switch view
if v.tag == 1001 {
// One last check we cast the view to UISwitch if it succeed then it's the switch view
if let mySwitch = v as? UISwitch {
if(mySwitch.isOn == true){
descr_label?.text = "\(cell?.textLabel?.text ?? "")"
print("The cell has the Switch On")
switch_isOn = true
}else{
descr_label?.text = "\(cell?.textLabel?.text ?? "")"
switch_isOn = false
print("The cell has the Switch Off")
}
}
}
}
self.performSegue(withIdentifier: segueIdentifier, sender: self)
}
}
Using the accessory view for the switch seems to be an easy solution but it's very cumbersome to access the view. Something like for v in cell?.subviews ?? [] and dealing with tags is horrible.
A better more efficient solution is a custom cell class.
In Interface Builder set the style of the cell to custom and drag an UILabel and an UISwitch into the canvas. Set the class of the cell to TableViewCell.
Add a new CocoaTouch class TableViewCell as subclass of UITableViewCell. You need two IBOutlets, one IBAction and a callback variable. The callback is important to keep the state of the switch in the model. Connect the outlets and the action in IB.
class TableViewCell: UITableViewCell {
#IBOutlet weak var switcher : UISwitch!
#IBOutlet weak var label : UILabel!
var callback : ((Bool)->())?
#IBAction func switchChanged(_ sender : UISwitch) {
callback?(sender.isOn)
}
}
Create a data source model containing the text and the state of the switch
struct Item {
var text : String
var isSelected : Bool
init(text : String, isSelected : Bool = false {
self.text = text
self.isSelected = isSelected
}
}
Declare the data source array
var arr : [Item] = [Item(text: "Text1"), Item(text: "Text2"), Item(text: "Text3"), Item(text: "Text4")]
Replace cellForRow with
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
let item = arr[indexPath.row]
cell.label.text = item.text
cell.switcher.isOn = item.isSelected
// the callback updates the model and is called when the value of the switch changes
cell.callback = { newValue in
item.isSelected = newValue
}
return cell
}
Replace didSelectRow with (yes, it's only one line, it passes the index path as sender parameter)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "segue", sender: indexPath)
}
Finally Implement prepare(for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
let viewController = segue.destination as! ViewController // the class of the second view controller
// get the current index path
let indexPath = sender as! IndexPath
let item = arr[indexPath.row]
// get the state of the switch from the model, not from the view
let isSelected = item.isSelected
// do something with `isSelected`
}
}
}
In order to achieve what you want properly, you're going to want to set up a custom cell.
An example of this is below, and assumes a Storyboard/XIB UI:
import UIKit
class SwitchTableViewCell: UITableViewCell {
#IBOutlet weak var textLabel: UILabel!
#IBOutlet weak var contentSwitch: UISwitch!
// So that we can identify the cell in our table view controller.
static let identifier: String {
return String(describing: type(of: self))
}
}
in order to use this with your table view. you will have to register the cell for use in SwitchTableViewController.viewDidLoad():
tableView.register(SwitchTableViewCell.self, forCellReuseIdentifier: SwitchTableViewCell.identifier)
Next, you're going to want to modify cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(SwitchTableViewCell.identifier, forIndexPath: indexPath) as! SwitchTableViewCell
cell.textLabel?.text = arr[indexPath.row]
// cell.contentSwitch will be setup as an outlet via Storyboard / XIB.
return cell
}
after that's done, go ahead and add a variable to SwitchTableViewController:
fileprivate var selectedState: UIControl.State?
And update didSelectRowAt to store the state from the cell:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! SwitchTableViewCell
selectedState = cell.contentSwitch.state
segueIdentifier = "segue" // probably want a more meaningful segue name here.
self.performSegue(withIdentifier: segueIdentifier, sender: self)
}
finally, override prepare(for:sender:):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
if let vc = segue.destination as? ContentViewController { // cast accordingly, 'ContentViewController' is placeholder
// pass the state to the destination view controller
vc.state = selectedState
selectedState = nil
}
}
}
and that's you done!
There are many ways to read the state of a switch from a cell. You can create a custom cell class and access that using IBOutlets and even you can use delegates to get back from the Custom Cell class back to your View Controller. If you're using this code for learning purposes it's Ok to use and add any types of Controls to the cell like this but in real project you might try Custom cells.
See the commented areas in the code
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var myimg: UIImageView!
var arr: [String] = ["bla", "blablabla", "blabla"]
override func viewDidLoad() {
super.viewDidLoad()
myimg?.image = UIImage(named: "Image1")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default,
reuseIdentifier: "cell")
cell.textLabel?.text = arr[indexPath.row]
let mySwitch = UISwitch()
// Add a tag to your switch so later on you can access the switch using this tag
mySwitch.tag = 1001
cell.accessoryView = mySwitch
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
segueIdentifier = "segue"
// Get the selected Cell and Iterate through it's subviews to find the switch using the tag
let cell = tableView.cellForRow(at: indexPath)
// Iterate through subviews of Cell
for v in cell?.subviews ?? [] {
// If a view found with tag == 1001 then it's the switch view because we had assigned 1001 to the switch view
if v.tag == 1001 {
// One last check we cast the view to UISwitch if it succeed then it's the switch view
if let mySwitch = v as? UISwitch {
// Here you can get the state of the switch
let switchState = mySwitch.state
}
}
}
self.performSegue(withIdentifier: segueIdentifier, sender: self)
}
}
As I said this is not the best way to add and read views using tags but still good to know that you can
Edit:
Here is the complete solution for your project to work. You already have a ViewController but you don't have a DetailViewController to which you want to segue
View Controller Code
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let rooms: [String] = ["Kitchen","Living Room", "Master's Bedroom", "Guest's Bedroom"]
let segueIdentifier = "segueIdentifier"
var switch_isOn = false
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rooms.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = rooms[indexPath.row]
let mySwitch = UISwitch()
cell.accessoryView = mySwitch
mySwitch.tag = 1001
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Get the selected Cell and Iterate through it's subviews to find the switch using the tag
let cell = tableView.cellForRow(at: indexPath)
// Iterate through subviews of Cell
for v in cell?.subviews ?? [] {
// If a view found with tag == 1001 then it's the switch view because we had assigned 1001 to the switch view
if v.tag == 1001 {
// One last check we cast the view to UISwitch if it succeed then it's the switch view
if let mySwitch = v as? UISwitch {
// Assign the current state of the switch to switch_isOn variable
self.switch_isOn = mySwitch.isOn
}
}
}
self.performSegue(withIdentifier: segueIdentifier, sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segueIdentifier {
if let detailViewController = segue.destination as? DetailViewController {
// used guard let to be on safe side
guard let indexPath = sender as? IndexPath else { return }
// pass in the data needs to the detail view controller
detailViewController.descr = rooms[indexPath.row]
detailViewController.isOn = switch_isOn
}
}
}
}
Detail View Controller Code
import UIKit
class DetailViewController: UIViewController {
#IBOutlet weak var descr_label: UILabel!
#IBOutlet weak var state_label: UILabel!
#IBOutlet weak var myImageView: UIImageView!
var descr = ""
var isOn = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
descr_label.text = descr
// for UIImage you can use UIImage(named: "on_image") but i have used the imageLiteral which is pics directly the image from xcassets
myImageView.image = isOn ? #imageLiteral(resourceName: "on_image") : #imageLiteral(resourceName: "off_image")
state_label.text = isOn ? "Switch is ON" : "Switch is Off"
}
}
Example Project download here

Loop through a uitableviewcell on submit in swift 4

I am trying to access each value of a text field in a prototype cell within a UITableView on Submit. I know I should be doing this in a better way (model) but for now, I just need to access these fields and cannot find a way to do this in Swift 3/4. Would anyone be able to assist?
Code:
import UIKit
import Firebase
class FormTableViewController: UITableViewController {
var formLabels = [String]()
var formPlaceholders = [String]()
override func viewDidLoad() {
super.viewDidLoad()
FirebaseApp.configure()
formLabels = ["Name","Email","Password", "Phone"]
formPlaceholders = ["John Smith","example#email.com","Enter Password", "8585551234"]
tableView.estimatedRowHeight = 30
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return formLabels.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier:
"FormTableCell", for: indexPath)
as! FormTableViewCell
let row = indexPath.row
cell.formLabel.font =
UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)
cell.formLabel.text = formLabels[row]
cell.formTextField.placeholder = formPlaceholders[row]
return cell
}
#IBAction func submitButtonPressed(_ sender: Any) {
// Need to do something with the Name, Email, Phone and Password fields here
}
}
You seem to acknowledge that updating the model directly probably makes sense. So why not do that? Just:
Have model collection for the responses;
Set up delegate for the text field in the cell;
Have cellForRowAt set that delegate; and
Make the table view controller conform to that class.
So, something quick and dirty, set up the cell to hook up editChanged event from the text field and set up protocol to inform the view controller:
protocol FormTableViewCellDelegate: class {
func fieldValueChanged(cell: UITableViewCell, textField: UITextField)
}
class FormTableViewCell: UITableViewCell {
weak var delegate: FormTableViewCellDelegate?
#IBOutlet weak var formLabel: UILabel!
#IBOutlet weak var formTextField: UITextField!
#IBAction func editingChanged(_ sender: UITextField) {
delegate?.fieldValueChanged(cell: self, textField: sender)
}
}
And then have the view controller set up model object and conform to your new protocol:
class FormTableViewController: UITableViewController {
var formLabels = [String]()
var formPlaceholders = [String]()
var values = [String?]()
override func viewDidLoad() {
super.viewDidLoad()
...
formLabels = ["Name","Email","Password", "Phone"]
formPlaceholders = ["John Smith","example#email.com","Enter Password", "8585551234"]
values = [nil, nil, nil, nil]
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FormTableCell", for: indexPath) as! FormTableViewCell
let row = indexPath.row
cell.formLabel.font = .preferredFont(forTextStyle: .headline)
cell.formLabel.text = formLabels[row]
cell.formTextField.placeholder = formPlaceholders[row]
cell.formTextField.text = values[row]
cell.delegate = self // set the delegate, too
return cell
}
#IBAction func submitButtonPressed(_ sender: Any) {
print(#function, values)
}
}
// delegate protocol to update model as text fields change
extension FormTableViewController: FormTableViewCellDelegate {
func fieldValueChanged(cell: UITableViewCell, textField: UITextField) {
guard let indexPath = tableView.indexPath(for: cell) else { return }
values[indexPath.row] = textField.text
}
}
Then that's it, your model is updated as the text fields are updated. Plus this has the advantage that it now supports cell reuse, conforms to MVC patterns, etc.
If you want to just loop through cells, you can create an array of ‘IndexPath’.
let array = (0..<formLabels.count).map { IndexPath(row: $0, section:0) }
After that you can loop over this array and access individual cell using tableview method:- tableView.cellForIndexPath
Hope this helps. (Not on my laptop, so didn’t test the syntax)

When scroll table view content change

i have custom table view cell that having rating stars. i'm using https://github.com/hsousa/HCSStarRatingView for rating View.
there is my code for table view and cell view.
class RatingTableViewCell: UITableViewCell {
var value : CGFloat = 0.0
#IBOutlet weak var starRatingView: HCSStarRatingView!
#IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
initStarRatingView()
starRatingView.addTarget(self, action: #selector(DidChangeValue(_:)), for: .valueChanged)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
private func initStarRatingView() {
var scalingTransform : CGAffineTransform!
scalingTransform = CGAffineTransform(scaleX: -1, y: 1);
starRatingView.transform = scalingTransform
starRatingView.emptyStarImage = #imageLiteral(resourceName: "strokStar")
starRatingView.halfStarImage = #imageLiteral(resourceName: "halfStar")
starRatingView.filledStarImage = #imageLiteral(resourceName: "fillStar")
starRatingView.allowsHalfStars = true
}
#IBAction func DidChangeValue(_ sender: HCSStarRatingView) {
self.value = sender.value
}
class RatingViewController: CustomViewController,UITableViewDelegate,UITableViewDataSource {
var values : [CGFloat] = [0.5,0.0,0.0,0.0,0.0,0.0,0.0]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RatingTableViewCell", for: indexPath) as! RatingTableViewCell
values[indexPath.row] = cell.value
cell.starRatingView.value = values[indexPath.row]
return cell
}
//MARK: _Table data source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
values.count
}
}
there is a problem when i scroll table view. dequeue Reusable Cell data is wrong. how can update value data for each cell?
The problem is that you are storing the value in the cell. Take a look at those two lines:
let cell = tableView.dequeueReusableCell(withIdentifier: "RatingTableViewCell", for: indexPath) as! RatingTableViewCell
values[indexPath.row] = cell.value
You dequeue a cell and assign it's value to values[indexPath.row]. The problems that you are noticing when scrolling are caused by the fact that the reused cell was previously used for a different indexPath, which means that their value (that you assign to values[indexPath.row]) is meant for its previous indexPath.
To fix that, I would advise getting rid of the value variable in RatingTableViewCell. Instead, define a protocol RatingTableViewCellDelegate that will be used to inform the RatingViewController about the new value.

Dynamically resize TableViewCell with and with out images

Using swift3, I want to allow users to create posts of either pictures or simple text posts. I have everything working fine except for when I create a post of just text, the UIImageView in the cell fills out space in the TableViewCell. Ideally, if the user creates a post of just text, the TableViewCell will only include everything down to the caption label but not the UIImageView (see image). How can I go about this.
Research: https://www.youtube.com/watch?v=zAWO9rldyUE,
https://www.youtube.com/watch?v=TEMUOaamcDA, https://www.raywenderlich.com/129059/self-sizing-table-view-cells
Current Code
func configureCell(post: Post){
self.post = post
likesRef = FriendSystem.system.CURRENT_USER_REF.child("likes").child(post.postID)
userRef = FriendSystem.system.USER_REF.child(post.userID).child("profile")
self.captionText.text = post.caption
self.likesLbl.text = "\(post.likes)"
self.endDate = Date(timeIntervalSince1970: TimeInterval(post.time))
userRef.observeSingleEvent(of: .value, with: { (snapshot) in
let snap = snapshot.value as? Dictionary<String, Any>
self.currentUser = MainUser(uid: post.userID, userData: snap!)
self.userNameLbl.text = self.currentUser.username
if let profileImg = self.currentUser.profileImage {
self.profileImg.loadImageUsingCache(urlString: profileImg)
} else {
self.profileImg.image = #imageLiteral(resourceName: "requests_icon")
}
})
// This is where I belive I need to determine wether or not the cell should have an image or not.
if let postImg = post.imageUrl {
self.postImg.loadImageUsingCache(urlString: postImg)
}
I see you're using storyboard to create your UI, in that case you can add a height constraint to your UIImageView (make sure to connect it your cell in order to use it in code) and change the constraint and the height of your tableview when required.
class MyCell: UITableViewCell {
#IBOutlet var postImage: UIImageView!
#IBOutlet var postImageHeight: NSLayoutConstraint!
}
class ViewController: UITableViewController {
var dataSource: [Model] = []
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//Cell without image
if dataSource[indexPath.row].image == nil {
return 200
}
//Cell with image
return 350
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
//Adjust the height constraint of the imageview within your cell
if dataSource[indexPath.row].image == nil {
cell.postImageHeight.constant == 0
}else{
cell.postImageHeight.constant == 150
}
return cell
}
}

IOS swift how can I get label text inside a TableView on label tap

I have a TableView that has data inside of it via Labels. When you click the label the Tap registers but now I would like to get the Data of the clicked label and I am having a difficult time getting that done. I have the same functionality working for Buttons for instance this is how I do it for my buttons inside a TableView .
Button Click Event
var locations = [String]()
#IBOutlet weak var Location: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
TableSource.dataSource = self
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Registration_Search", for: indexPath)
cell.Location.setTitle(locations[indexPath.row], for: UIControlState.normal)
cell.Location.addTarget(self, action: #selector(Registration_SearchController.Location_Click(sender:)), for: .touchUpInside)
cell.Location.tag = indexPath.row
return cell
}
func Location_Click(sender: UIButton) {
print(locations[sender.tag])
}
That code above allows me to get the Data of any Button that is clicked . I now try to do the same for the Label but can't get the data that the Label has . This is my Code for the Label and oh the same are the same as above but different ViewController
var locations = [String]()
#IBOutlet weak var location: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
TableSource.dataSource = self
location.isUserInteractionEnabled = true
}
func tapFunctionn(sender: UITapGestureRecognizer)
{
// I would like to get the data for the tapped label here
print("Tapped")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Registration_Search", for: indexPath)
cell.location.text = Locations[indexPath.row]
let tap = UITapGestureRecognizer(target:self, action: #selector(HomePageC.tapFunctionn))
cell.location.addGestureRecognizer(tap)
return cell
}
Again when I click the label it prints Tapped but can't get the actual data. In the Button function I could use Sender.Tag but the UITapGestureRecognizer does not have a Tag method . Any suggestions would be greatly appreciated
You can make something like that:
func tapFunctionn(recognizer: UIPinchGestureRecognizer) {
let view = recognizer.view
let index = view?.tag
print(index)
}
You don't have to use UITapGestureRecognizer. Just use the delegate method. Set the UITableView delegate to your UIViewController and make the class conform to the UITableViewDelegate
For swift 3
override func viewDidLoad() {
super.viewDidLoad()
TableSource.dataSource = self
TableSource.delegate = self
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
//access the label inside the cell
print(cell.label?.text)
//or you can access the array object
//print(Locations[indexPath.row])
}

Resources