accessory view. Checkmark - ios

I have a tableview and each cell has a switch as accessory view. I can iterate through all cells to see which ones have switch on.
I can easily change the accessory view to be a check mark.
I need to implement that tapping each cell toggles between checkmark and none.
Then in touchupinside, iterate through all cells to see which ones are checked and which ones are not checked.
I have implemented to check for Switch state
var indexPaths = table.IndexPathsForVisibleRows;
foreach (var indexPath in indexPaths)
{
var cell = table.CellAt (indexPath);
var switchView = cell.AccessoryView as UISwitch;
if (switchView.On)
{
/*code to handle switch on*/
}
}
I dont know how to have similar code to check if accessoryview is Checkmark and if it is set to none

Here's an example that uses MarkupKit's LMTableView class to implement selection management:
http://gkbrown.org/2015/08/10/using-lmtableview-to-implement-radio-button-like-behavior-in-ios-applications/
Essentially, you define a particular section of a table view as being either single-select or multi-select, and the LMTableView class manages the selection state for you. You can then query the table view to find out which rows are selected, or you can respond to selection events yourself via table view delegate methods.

Related

Preview a view in a table view cell from selecting a row on touch

In my table view cell I have a UIView that shows 5 stars rating. Users can tap it to change the rating. The problem is, when users change the rating the didSelectRow on the table view row is triggered. What is the best way of preventing this? Is there a way to block the UIView from passing the touch events to the table view? I still want the table view cell to be tappable outside the rating view.
1.If you are using buttons in ratting view for ratting?
if yes then increase button size
2.Otherwise you have to increase cell height for ratting view clickable.
I request you to provide screenshot of your tableview
and Answer if you are using any library for ratting.
You could override UIResponder's next property to break the responder chain:
extension CosmosView {
open override var next: UIResponder? {
return nil
}
}
See https://developer.apple.com/documentation/uikit/uiresponder/1621099-next.

Changing singleton content

I have two view controllers, one is of table view and another one is to display the content of selected cell from table view. Now both view controller and tableview cell has textfields, if change the text of textfield in content displaying view controller I want same change in respected tableview cell when I came back to tableview. Note that, the data is accessing from singleton object. I tried to change the value in singleton, changes are done inside the view controller it does not effecting tableview cell.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
resignFirstResponder()
if(textField == carRating){
let rating = Float(textField.text!)
secondInstanceForMyData.masterCarData[index].carRating = rating
}
return true
}
Here carRating is an element of singleton instance "secondInstanceForMyData". This code is in viewController for selected row in table view. Now after changing the value if go back to table view there is no change in respected cell. How can I achieve change in respected table cell, I have index value for selected cell.
You might just need to call reloadData or reloadRowsAtIndexPaths:withRowAnimation: on the UITableView when you go back to it.
This assumes you definitely have a singleton you are working with -- secondInstanceForMyData is suspiciously named.

displaying checkmarks for the selected cells after adding new cell - Swift

I have a UITableView with UITableViewCell's and once i select a cell it will display a checkmark for that cell until here its fine, but once i press the add button to add a new cell to that UITableView it will take me to another ViewController to write the text of the new cell and add it, so after adding it it will take me to the previous UITableView but without displaying the checkmarks to the cells that i have already selected before adding the new cell, is there a way to display checkmarks for the selected cells after pressing the back button from the other controller ?
Note: I am using Xcode with swift language to build my application.
The cells in your tableView is made with a array of some Object. In that Object you need to add a variable isChecked, and if it true, the tableviewcell at that indexpath will be checked.
you can do so by adding something like this in cellforrowatindexpath
if objectAtIndexPath.isChecked {
cell.accessoryType = .Checkmark
} else {
cell.accessoryType = .None
}
So the key is to have a have a variable in the object you are using to populate the viewcontroller.
When you create the new item, you simple say isChecked = true, and then add it to the array which contains the items, and it will be checked automatically.

How can I select two or more cells(UITableViewCell) at the same time

My cell(UITableViewCell) contains a UITextView, and I have customed UIMenuController(Clipboard). Now I need select two or more cells at the same time, so that I can copy the text of them. What should I do?
Use this line for multiple selection.
self.tableView.allowsMultipleSelection = YES;
Allow multiple selection on your tableview by setting the property
self.tableView.allowsMultipleSelection = YES;
Then, in your didSelectRowAtIndexPath, you could manage a collection of data by storing or removing the UITextView's content based on what is returned by
if (cell.selected) {
// Add to collection
} else {
// Remove from collection
}
In the case that multiple cells are selected when you present your UIMenuController, you could manipulate the menu text to say something like
Copy text from (#) items
and then use your collection to grab the data regardless of which cell they accessed the menu from.
It would be helpful for the user to see some indication of multiple selection. For this you could set the accessoryType on the selected cell to UITableViewCellAccessoryCheckmark.
To deselect the row, set it back to UITableViewCellAccessoryNone.

How to keep track of selected Item in a list being represented by tableView?

If I select a row, and rotate the device, I have a completely different layout for orientations.
I would like to keep track of last selected item which I can re-do after rotation in the new layout.
So, should I ideally store the selected indexPath, selected row number or the selected model object being represented in each cell from the dataSource ?
I tried storing the selected cell, but it it creates problem when reused.
If you use same data for new layout, saving the selected indexPath would be enough I think so. But if you use data differently, means it can be at different index (if you do some process after rotation), then you need to save that as well.
EDIT
Before creating new layout for new orientation, find selected indexpath of selected cell and save it. Then in cellForRowAtIndexPath:, check if it is present at that indexPath and make its state as selected. Hope it helps you.
First initialize a NSMutableArray *selectedItems. (alloc-init in viewDidLoad)
In didSelectRowAtIndexPath delegate method, check
if ([selectedItems indexOfObject:indexPath] == NSNotFound) // if not available
{
[selectedItems addObject: indextPath]
}
// if you are having deselect option as well then you can have else statement as well
else
{
[selectedItems removeObject: indexPath];
}
So now in your cellForRowAtIndexPath while cell initialization,
first draw a normal cell with all properties and at the end check:
if ([selectedItems indexOfObject:indexPath] != NSNotFound) // if current indexpath is selected
{
// then display your selected cell [checkbox or different bg color as per your requirements]
}
Hope this helps you.

Resources