get textLabel of selected row in swift - ios

I want it to draw the data I have selected in the application. for example, I want the data in row 0 to pull the data in row 1. these data are kept as follows.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DeviceTableViewCell
let deviceItem: Device = items[indexPath.row]
cell.deviceItem = deviceItem
cell.title.text = deviceItem.title
cell.button.isOn = deviceItem.state
return cell
}
So I want to take the device names. cell.title.text = deviceItem.title I want to draw the lines here.

Use below code to get of selected row:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let deviceItem: Device = items[indexPath.row]
print(deviceItem.title)
}

Related

How do I have a single select and a multiselect in my tableview?

in my cellForRowAt Im inflating my xib and setting its content:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! TableViewCell
cell.label.text = data[indexPath.row]
let tap = UIGestureRecognizer(target: self, action: #selector(tableCellTap(_:cell:)))
cell.addGestureRecognizer(tap)
cell.selectionStyle = .none
cell.checkBox.isUserInteractionEnabled = false
cell.checkBox.boxType = .circle
cell.checkBox.markType = .radio
cell.checkBox.cornerRadius = 0
cell.checkBox.stateChangeAnimation = .expand(.fill)
if (indexPath.row == selectedIndex){
cell.checkBox.checkState = .checked
}else{
cell.checkBox.checkState = .unchecked
}
return cell
}
Then Im setting my deselect and select values
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
cell.checkBox.checkState = .checked
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
cell.checkBox.checkState = .unchecked
}
I need a sense of direction in how i can make it select and deselect the same row and update my xib file checkbox?
m13checkbox is being used
when i click on the cell for the first time it selects it but when i click on the same cell again it does not call it and does nothing until a loop a completed in the checkboxes
you don't need didDeselectRowAt, you can achieve the same functionality by checking if the radio button is checked in your didSelectRowAt.
If the radio button is checked, simply uncheck it and vice versa.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
if(cell.checkBox.isChecked){
cell.checkBox.checkState = .unchecked
}else{
cell.checkBox.checkState = .checked
}
}
Apple has already given very good sample code for that on its website,
you can check at:https://developer.apple.com/library/content/samplecode/TableMultiSelect/Introduction/Intro.html

UITableView cell on double click should go to previous state

If I select (click) row of TableView, it should add Image saying that I selected this particular Item. It's working fine!
My problem is: if user want to move back from that selected item.
If I click on the same row it should deselect that cell and hide that image.
What I tried is:
func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as! leistungCell
// Configure the cell...
let tableData = self.tableData[indexPath.row]
cell.leistungLbl.text = tableData["leistung_info"] as? String
//space between Rows
cell.contentView.backgroundColor = colorLightGray
cell.contentView.layer.borderColor = UIColor.white.cgColor
//space between Rows
cell.contentView.layer.borderWidth = 3.0
cell.contentView.layer.cornerRadius = 8
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.imageView?.image = UIImage(named: "check.png")
let value = Info["leistung_info"] as! String
}
func tableView(_ tableView: UITableView, didDeSelectRowAt indexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)
cell?.imageView?.image = nil
}
Forget and remove didDeSelectRowAt, just use didSelectRowAt, and an array to save selections:
var selectedIndexPaths = [IndexPath]()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if let index = selectedIndexPaths.index(of: indexPath) { //deselect it if the row is selected
tableView.deselectRow(at: indexPath, animated: true)
cell?.imageView?.image = nil
selectedIndexPaths.remove(at: index)
}
else{ //select it if the row is deselected
cell?.imageView?.image = UIImage(named: "check.png")
selectedIndexPaths.append(indexPath)
}
}
And be aware of that the cells are being REUSED ! Please do the same check in cellForRowAt method.

how to add Checkmark TableView Multiple Swift 3.0

I want to make the user multiple choice (years) or choose (all) and the rest of the cells disappear (checkmark).
I want the user can not cancel each (checkmark).
This My Code :-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! yearsCell
cell.years?.text = Years[indexPath.row]
return cell }
The way I do this is by storing the selectedindexPath's in an array, then reload the tableView to change the cells that were selected, this way you avoid problems with cell re-use, this might look like this (un-tested).
var selectedIndexPathArray = Array<NSIndexPath>()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
selectedIndexPathArray.append(indexPath)
tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! yearsCell
cell.years?.text = Years[indexPath.row]
cell.accessoryType = .none
for sip in selectedIndexPathArray {
if indexPath == sip {
cell.accessoryType = .checkmark
}
}
return cell
}
Like So:
SWIFT 3
// To checkmark the cell
let cell = tableView.cellForRow(at:indexPath.row)
cell.accessoryType = .checkmark
// To make it unselectable
Use the tableView:willSelectRowAtIndexPath: method and check if the indexPath.row is the row for the cell you just selected, and return false.
The cell is reusable. For example, the cell used to display 1st row might be used to display 10th row when scroll down. Therefore, you need a array store the checkmark status for each row, some code like:
didSelect: array[indexpath.row] = !array[indexpath.row]
cellForRow: setCheckmark(array[indexpath.row])
MORE: google how and why a cell is reused in tableview

How to display each elements of loop to table row?

how to return each element of loop. I mean i want display each name of this loop to row text. It is only return last element. How can i return all of this?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellFullname = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
for last in lastnames {
cellFullname.textLabel?.text = "\(last)"
}
return cellFullname
}
You don't need a loop for displaying the elements in UITableView
Say you have an array of last names:
var lastnames = ["....
And you want to put each element in a UITableViewCell. Two steps:
Define the amount of cells you need:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lastnames.count
}
Update the UITableView with the names:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellFullname = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
cellFullname.textLabel?.text = lastnames[indexPath.row]
return cellFullname
}
Just change the part where you're assigning the textLabel.text :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellFullname = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
cellFullname.textLabel?.text = lastnames[indexPath.row]
return cellFullname
}
You better create an array of elements arr[].
Use this :
cellFullname.textLabel?.text = arr[index.row]
Updated:
Just access each element of lastnames using indexPath.row.
let cellFullname = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
cellFullname.textLabel?.text = lastnames[indexPath.row]
return cellFullname
}
If you will add for loop with in cellForRowAt then for each cell creation that loop will run, which not good as well.

Change image on select within tableview cell - Swift

I am trying to change an image within a cell when it is selected. Here is what I have in my view controller:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell
cell.bgImage.image = UIImage(named: "second_image")
}
This is where I set the image:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell
cell.bgImage.image = UIImage(named: "first_image")
}
When I select the cell, the image does not update. How do I get this to work?
The issue is probably that you are calling dequeueReusableCellWithIdentifier. Try using cellForRowAtIndexPath instead (documentation about it here). This will give you a reference to the current cell, rather than a reference to a new reusable cell.
So, according to your code above:
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
This typecast is critical because cellForRowAtIndexPath returns UITableViewCell, which will not have the bgImage member.
Also, make sure that you have a member called bgImage that is a UIImageView in your CustomTableViewCell class by adding #IBOutlet strong var bgImage: UIImageView!, and that said IBOutlet is connected in your storyboard/xib.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
cell.bgImage.image = UIImage(named: "second_image")}
For Swift 3+:
func tableView(tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
cell.bgImage.image = UIImage(named: "second_image")}
Change your didSelectRowAtIndexPath to:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell.bgImage.image = UIImage(named: "second_image")
}
As per default this works to change the image
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell!.imageView?.image = UIImage(named: "2")
Though it is too late. But i like to answer to participate in this world.
I was having problem with changing selected and deselected cell image of a table view. I solved it this way.
When tableview delegate method call then create instance of that cell by
let cell = self.tableView.cellForRow(at: indexPath) as! CustomeCell
instead of
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomeCell
and use your desired image into your cell imageview.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.tableView.cellForRow(at: indexPath) as! CustomeCell
if indexPath.row == 0
cell.btnImageView.image = UIImage.init(named: "screenr.png")
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = self.tableView.cellForRow(at: indexPath) as! CustomeCell
if indexPath.row == 0
cell.btnImageView.image = UIImage.init(named: "screen.png")
}
Can't you simply use the .hightlightedimage value for the imageView? I'm doing the following in my cellForRowAt func:
cell.imageView?.image = UIImage(named: "\(indexPath.row).png")
cell.imageView?.highlightedImage = UIImage(named: "\(indexPath.row)g.png")
And I've just named the necessary images "0.png" and "0g.png" etc to make sure the image and highlighted image match up. Of course, that's a small static array, if you were using coreData with a larger array you could simply hold the imageName and the hightlightedImageName in the data set and pull the info from there.

Resources