How to make selection style in UITableViewCell - ios

I created Xib file for my uitableviewcell in the attributed inspector i choose SelectionStyle default, but it didn't help me. So and i added accessory type .detailDisclosureButton.
So when i tapped on a cell , My cell doesn't highlighted gray color, only disclosurebutton does

Maybe you can try this one,
extension CWScoreBoardViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
or you can try this,
extension CWScoreBoardViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CWUsersTableViewCell", for: indexPath) as! CWUsersTableViewCell
cell.selectionStyle = .none
return cell
}
}

Related

UITableView : Strange checkmark accessory type behavior

A gif to introduce my problem :
In my UITableView I set a checkmark accessory type but a white background appears instead of displaying the checkmark of the cell. The checkmark is here, but why do I have this white background ?
The tint color of my cell is set to white, changing it just change the color of the checkmark and the background is still here.
My code :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? CheckTableViewCell {
if cell.titleLabel.text != "" {
cell.accessoryType = .checkmark
}
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? CheckTableViewCell {
cell.setUp(withTitle: dataSource[indexPath.row].title)
return cell
}
return CheckTableViewCell()
}
func setUp(withTitle title : String){
self.titleLabel.text = title
}
Any idea ?
I had to set both cell content's View and UITableView background color to pink in StoryBoard to make it works.

DidselectRow delegate method not working in swift

I have an alert in which I have used table view to show some data.
User can select any item from the table view data.
Now when I'm trying to select any item in didSelectRow method it isn't selecting an item. I have used breakpoints also but it isn't catching the breakpoints also.
I have given its delegates and datasources everything is fine but i'm confused why it isn't working?
My code is this:
extension YourOrdersViewController : UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PreOrderService.instance.PreOrderModelInstance.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Hello")
print(PreOrderService.instance.PreOrderModelInstance[indexPath.row].perorder_time)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if tableView == orderTableView{
print("Dishes")
}
else{
orderTableView.cellForRow(at: indexPath)?.accessoryType = .none
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = orderTableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath) as!
PreOrderTableViewCell
cell.titleLbl.text = PreOrderService.instance.PreOrderModelInstance[indexPath.row].perorder_time
print(cell.titleLbl.text!)
cell.selectionStyle = .none
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.accessoryType = cell.isSelected ? .checkmark : .none
return cell
}
}
View Controller looks like this,
enter image description here
Check or you have set this value:
If No Selection is set instead of Single Selection, the func tableView (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) method will not be executed.
Does it have any gesture recognizer on your UIViewControlller. If it does and the gesture recognizer is above your table views on responder chain, it will take any single touch and leave nothing for your table views.
Whole problem in your code I think is:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
self.tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
In Main.storyboard select your table view and in Attribute inspector change selection to single selection
Make sure that the delegate and datasource are set.
If you're creating the views via storyboard, make sure that you set the iboutlets for delegate/datasource.
Both of these lines mean the same thing: so remove the second one.
cell.selectionStyle = .none
cell.selectionStyle = UITableViewCellSelectionStyle.none

CheckBox on tableview duplicating, Swift, iOS

I have a tableView that when selected changes an image from one to another. This all works fine but when I select a tableCell it changes the image, but when I scroll it has also changed the image of another cell that I didn't select.
Below is my code.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FeaturesCell") as! FeaturesCell
cell.featuresLabel.text = self.items[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
pickedFeatures.append(items[indexPath.row])
let cell = tableView.cellForRow(at: indexPath) as! FeaturesCell
cell.checkImage.image = #imageLiteral(resourceName: "tick-inside-circle")
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
pickedFeatures.remove(at: pickedFeatures.index(of: items[indexPath.row])!)
let cell = tableView.cellForRow(at: indexPath) as! FeaturesCell
cell.checkImage.image = #imageLiteral(resourceName: "No-tick-inside-circle")
}
If I use detqueureusable cell in the did select function then it just doesn't change the picture at all when selected.
You can use tableView.dequeueReusableCell(_), The problem is, you didn't maintain the status of the selected cells.
Example :
class viewController: UIVieWController, UITableViewDelegate, UITableViewDataSource {
var selectedCellList = [IndexPath]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FeaturesCell") as! FeaturesCell
cell.featuresLabel.text = self.items[indexPath.row]
if let _ = selectedCellList.index(of: indexPath) {
// Cell selected, update check box image with tick mark
cell.checkImage.image = #imageLiteral(resourceName: "tick-inside-circle")
} else {
// Cell note selected, update check box image without tick mark
cell.checkImage.image = #imageLiteral(resourceName: "No-tick-inside-circle")
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
pickedFeatures.append(items[indexPath.row])
if let index = selectedCellList.index(of: indexPath) {
selectedCellList.remove(at: index)
} else {
selectedCellList.append(indexPath)
}
tableView .reloadRows(at: [indexPath], with: .automatic)
}
}

Change image on selection in UITableViewCell(swift 3 xcode)

I have an imageView inside my tableViewCell and i would like to have its image changed on selection. This is the code I have for it:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let myCell = tableView.cellForRow(at: indexPath) as! TableCell
myCell.resourceIcons.image = UIImage(named: "RubiusResources2")
tableView.deselectRow(at: indexPath, animated: true)
}
The code works, but some of the other rows in a different section further down the tableView also seem change.
EDIT:
Using the comments bellow I came to the following solution:
I first created a 2D bool array to the amount of sections and rows my table had and set them all to false.
var resourceBool = Array(repeating: Array(repeating:false, count:4), count:12)
I then created an if statement to check if the array at indexPath was false or true. This would be where the states of the image would change.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! TableCell
if (global.resourceBool[indexPath.section][indexPath.row] == false) {
myCell.resourceIcons.image = global.systemResourceImages[0]
} else if (global.resourceBool[indexPath.section][indexPath.row] == true) {
myCell.resourceIcons.image = global.systemResourceImages[1]
}
return myCell
}
Then, in the didSelectRow function I change the array at indexPath to true and reload the tableView data.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
global.resourceBool[indexPath.section][indexPath.row] = true
tableView.reloadData()
tableView.deselectRow(at: indexPath, animated: true)
}
From my understanding, the states of an object must always be in the cellForRow.
One of the solution would be that you need to maintain a seperate list of rows you selected, compare them in cellForRowAt method.
Code would look something like this.
var selectedArray : [IndexPath] = [IndexPath]()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let myCell = tableView.cellForRow(at: indexPath) as! TableCell
myCell.resourceIcons.image = UIImage(named: "RubiusResources2")
tableView.deselectRow(at: indexPath, animated: true)
if(!selectedArray.contains(indexPath))
{
selectedArray.append(indexPath)
}
else
{
// remove from array here if required
}
}
and then in cellForRowAt, write this code to set proper images
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
.
.
.
if(selectedArray.contains(indexPath))
{
// use selected image
}
else
{
// use normal image
}
.
.
.
}

Segue is not working from static UITableView

I have a TableViewController with static cells
I tried both creating segue from TableViewController and TableView cell (Not at the same time)
However, in both scenario, didSelectRowAtIndexPath was not fired
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("select")
}
I also have embedded collectionviewcontroller
class EventDetail: UITableViewController, UITextFieldDelegate, UICollectionViewDataSource, UICollectionViewDelegate {
What may be causing this?
Check your tableview selection must be single selection, if it is "No selection" then didSelectedRowAtIndex would not get called.
You can download sample code and observe it.
Sample download
Also check
Ideally your cellForRowAtIndex should be like this
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell : CustomCell! = tableView.dequeueReusableCellWithIdentifier("ID_CustomCell", forIndexPath: indexPath) as! CustomCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.lblData.text = "CustomCell....."
return cell
}
Swift 4 code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CustomCell! = tableView.dequeueReusableCell(withIdentifier: "ID_CustomCell", for: indexPath) as! CustomCell
cell.selectionStyle = .none
cell.lblData.text = "CustomCell....."
return cell
}
call performseguewithidentifier from didselectRow if you have created segue from tableviewcontroller. like,
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("select")
self.performSegueWithIdentifier("your segue identifier", sender: self)
}
hope this will help :)

Resources