Does anybody know if this application is using UITableView and have a UITextView inside it? If so, i want to ask how this is done. I also was wondering how a single tap can present an datepicker. I've uploaded a picture for clarity of what i want to achieve.
It can be either a label or a text field. In terms of presenting a date picker, you would use the function didSelectRowAt like kid_x said. I did this on an app I'm building with the help of this site. First I declared var datePickerVisible = false in the ViewController Class
and using the picture as an example, I toggled the visibility of the Date Picker like so
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 1 {
tableView.beginUpdates()
datePickerVisible = !datePickerVisible
tableView.endUpdates()
}
tableView.deselectRow(at: indexPath, animated: true)
}
Related
I am having trouble selecting table view row in didSelectRowAt/didSelectRowAtIndexPath. The function is never called.
func tableView(tableView: UITableView, didSelectRowAt indexPath: NSIndexPath) {
print("Row: ", indexPath.row)
if(indexPath.row == 9)
{
print("Row 9 Selected")
}
}
Based on various Stack Overflow articles, here is what I have tried:
I have tried "didSelectRowAt" and "didSelectRowAtIndexPath"
I have set the table view's delegate to self using its outlet
Table View Settings:
-Selection: Single Selection
-Editing: No Selection During Editing
-Interaction: User Interaction Enabled
Individual Cell Settings:
-Interaction: User Interaction Enabled
I set tableview.allowsInteraction to true in code as well as storyboard
Should be with _
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Check Docs
The problem was that I was using NSIndexPath instead of IndexPath
check delegate value using break point either it is nil or not
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Code
}
You have to set an #IBOutlet to the tableView in you ViewController and set as it's delegate and dataSource.
Something like this :
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
And implements the UITableViewDataSource protocol too.
Or you can too in the Interface Builder set the ViewController as it's delegate and dataSource and avoid to set manually in code like above.
If you have more than one tableView I your code than please try to mention different names for every tableView and then call the delegate with particular tableView name.
Also you have mentioned 3rd point here “Single Selection -Editing: No Selection During Editing”. This means if you try to edit anything in tableView there will be no selection please try to replace this and then check if you get some result.
Because if set “tableView.allowsSelectionDuringEditing = false” then it will not allow you selections in tableView.
I would like to create a custom UITableViewCell which works like the phone number cell in the Contacts App:
Is there some build in method to the default controls like the add / delete / disclosure buttons to my cell? Or do I have to create these assets myself?
Of course creating and using my own assets (e.g. using a simple button with an image) is not a big deal. However, using some build in controls / assets would have the big advantage that future design updates would become visible in my app automatically.
Yes, there are built in controls. Here you have Apple tutorial where you have described how to create delete functionality.
Also in Apple documentation about UITableCellView you can find Editing Style property which is adding controls to cells.
Yes. You can implement the method below:
At first, the ViewController has a property named "isEditing", which decides whether the cell is in the editable status.
extension UIViewController {
open var isEditing: Bool
open func setEditing(_ editing: Bool, animated: Bool)
open var editButtonItem: UIBarButtonItem { get }
}
Then you must implement UITableView delegate methods like:
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
At last, when the variable "isEditing" is true, you can get what you want above.
I am trying to achieve to make my TableView only accept one checkbox per cell.
I have used this solution to save User Defaults for the checkmarks which works fine but it allow multiple selections.
I have tried so many solutions on Stack overflow but since this is formatted a little different I was unable to use those solutions.
Things I have tried which did not work:
Tried setting the accessory type to none at the didDeselectRowAt
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
Tried setting the table view to not allow multiple selections on the viewDidNotLoad
override func viewDidLoad()
{
tableViewName.allowsMultipleSelection = false
}
Based the solution I linked, how would I go to only allow a single selection?
In the solution you have linked there is this code in the 'didSelect' function
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if selectedRows.contains(indexPath.row) {
self.selectedRows = selectedRows.filter{$0 != indexPath.row}
}else{
self.selectedRows.append(indexPath.row)
}
UserDefaults.standard.set(selectedRows, forKey: "selectedRows")
}
If you only want a single checkbox to be selected then you only want a single entity saved in your selectedRows object. Change the above code to:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
self.selectedRows = [indexPath.row]
UserDefaults.standard.set(selectedRows, forKey: "selectedRows")
}
Then every time a checkbox is selected it will clear out all the other selected checkboxes and select only the checkbox you just clicked.
You can see my code here:
#IBAction func edit(sender: UIButton?) {
let indexPath = NSIndexPath(forRow: 0, inSection: 1)
self.tableView(tableVw, editActionsForRowAtIndexPath: indexPath)
}
It seems to that you miss the key points of UITableView behaviour. Please consider to read official documentation first: https://developer.apple.com/reference/uikit/uitableview
You got it backwards, the
optional func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
function is called by the table view on it's delegate while you are supposed to implement it and return an array of custom actions you want displayed - it's not supposed to be called by you to signal something to the table view.
https://developer.apple.com/reference/uikit/uitableviewdelegate/1614956-tableview
When I tap on the cells of my table view, they darken to a grey color, and don't turn back to white until I tap on a different cell. Is there some sort of Boolean I have to set for it to not do that?
Here's a screenshot explaining my problem:
Links to other websites would be helpful, if it would mean a more detailed description. (Unless it's a super simple fix, then the right code or steps-to-take would be easier than a link.)
This is the default behaviour of UITableView.
You must call deselectRowAtIndexPath inside of didSelectRowAtIndexPath inside your UITableViewController class.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Check out the iOS Documentation for more information.
UITableView
UITableViewDelegate
Swift 3
Option 1: (Which I always use)
To give it fade out animation after selected with gray you can do this:
func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
Option 2:
To remove the highlight effect completely you can add this line to your cellForRowAt :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = .....
cell.selectionStyle = .none
return cell
}
You can do this a couple ways...
tableView.allowsSelection = false
You can set the tableView in xCode Storyboard to not have any selection under the fourth tab.
Or, you can do this on the cell cell.selectionStyle = UITableViewCellSelectionStyle.None
What you want is ultimately going to be about what behavior you are going after. Just do a little experimenting.
Swift 3
In a custom cell add this:
override func awakeFromNib() {
super.awakeFromNib()
selectionStyle = .none
}
This ensures you won't even see the gray when the cell is tapped. This code in the UITableViewDelegate only deselects when tapped.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
Just Simply click on the cell and go to attributes inspector you will find Selection Style , select none.
You can change style by:
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
Swift 4.1
tableView.deselectRow(at: indexPath, animated: true)
In the storyboard or xib,
In the Tableview cell,
you can select selection to "none",
in the atributes inspector