Swift - selectRowAtIndexPath not working with indexPathForSelectedRows - ios

When I use tableView.indexPathForSelectedRow(), calling selectRowAtIndexPath works perfectly. However, I am selecting multiple rows thus I am trying to use table.View.indexPathForSelectedRows() as [NSIndexPath]
Swift Compiler Error: NSIndexPath is not a subtype of NSIndexPath
Error has been resolved.
Edit Added For Loop, multiple selection still not happening.
//I initialized saveSelection globally
var saveSelection : [NSIndexPath]?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
saveSelection = (drugTableView.indexPathsForSelectedRows() as [NSIndexPath])
}
override func viewWillAppear(animated: Bool)
{
if !selectedDrugs.isEmpty
{
for save : NSIndexPath in saveSelection! as [NSIndexPath]
{
self.drugTableView.selectRowAtIndexPath(save, animated: true, scrollPosition: UITableViewScrollPosition.None)
}
}
}
Thank you for your help.

Here's what I did:
Made a dictionary of IndexPaths. Used didSelectRowAtIndexPath and didDeselectRowAtIndexPath to add and remove key/value pairs to the dictionary.
Inside viewWillAppear I used var indexForLoop = myDictionary.values.array to as the indexPath set to pass into my For Loop
Crossed my fingers and hoped for the best
thank you Lyndsey for your help!

Related

how to delete a cell from a UITableView with multi section in swift

i'm trying to delete a cell from UITableView in swift, i follow this tutorial: http://www.ioscreator.com/tutorials/delete-rows-table-view-ios8-swift
the problem is my UITableView has many section, so i can't delete the cell the way like the tutorial.
any one know how to delete cell form table with multiple section?
thanks.
You cannot delete multiple cells at once with the method described in the tutorial. That will only work for single cell. If you select multiple cells and use button, for example, to trigger delete action, your code could look something like this:
if let indexPaths = tableView.indexPathsForSelectedRows() as? [NSIndexPath] {
for indexPath in indexPaths {
// one by one remove items from your datasource
}
tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
}
Instead of using numbers[row] in the example you can use numbers[section][row]. So the code will look like:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numbers[section].count
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
numbers[indexPath.section].removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
Neither of answers worked for me. Swift Array indexes are updated upon removal hence for-in loop for indexes from .indexPathsForSelectedRows() provided unexpected results i.e. wrong data/tables removed and eventually crash with index outside of array bounds error. Found good (but really outdated) Objective-C iOS Developer Library example. But it utilised NSMutableArray removeObjectsAtIndexes method, not present with Swift Array. Anyway a good deal of useful tricks in there so worth take a look.
The method which work for me is part from that example but instead of removeObjectsAtIndexes do-while is used to remove rows one by one until all selected rows are removed. The method below called by UIAlertAction similar to Apple example.
func deleteSelectedRows() {
// Unwrap indexPaths to check if rows are selected
if let _ = tableView.indexPathsForSelectedRows() {
// Do while all selected rows are deleted
do {
if let indexPath = tableView.indexPathForSelectedRow(){
//remove from table view data source and table view
self.dataSource.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
} while tableView.indexPathsForSelectedRows() != nil
}else{
// Delete everything, delete the objects from data model.
self.dataSource.removeAll(keepCapacity: false)
// Tell the tableView that we deleted the objects.
// Because we are deleting all the rows, just reload the current table section
self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)
}
// Exit editing mode after the deletion.
self.tableView.setEditing(false, animated: true)
}
Edit: While do-while did it trick for small example I've been working with (jus starting with swift) It's not efficient. Either extending Array or make data source Equatable and use find() or .filter is preferable.But I'm sure there should be a simpler way. The one I'm using now is described on link below:
http://www.rockhoppertech.com/blog/swift-remove-array-item/
func == (lhs: myDataSource, rhs: myDataSource) -> Bool {
if lhs.data == rhs.data &&
lhs.otherData == rhs.otherData {
return true
}
return false
}
struct myDataSource: Equatable {
let data: String
let otherData: String
}
And then:
if let selectedRows = tableView.indexPathsForSelectedRows(){
var objectsToDelete = [myDataSource]()
for selectedRow in selectedRows {
objectsToDelete.append(myDataSource[selectedRow.row])
}
for object in objectsToDelete {
if let index = find(myDataSource, object){
myDataSource.removeAtIndex(index)
}
}
}
tableView.deleteRowsAtIndexPaths([selectedRows], withRowAnimation: .Automatic)
try to this. this works fine.
But don't forget to this before.
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == .Delete
{
arrayOfnames.removeAtIndex(indexPath.row)
self.tableViewww.reloadData()
}
}

collectionView: reloadData works, reloadItemsAtIndexPaths does not

I have a UICollectionView whose source data changes sometimes when offscreen. To update it when it returns, I wrote in the CollectionViewController:
override func viewWillAppear(animated: Bool) {
self.collectionView!.reloadData()
}
...and that works, but I never need to update more than one or two cells in my UICollectionView, so I thought it'd be better if I replaced the above version with:
override func viewWillAppear(animated: Bool) {
self.collectionView!.reloadItemsAtIndexPaths(myArrayOfIndexPaths)
}
...but when I do, Xcode gives me an error saying:
Operand of postfix '!' should have optional type, delete '!'".
When I delete the '!', it says that UICollectionView doesn't have a member named reloadItemsAtIndexPaths. What am I doing wrong?
From your code it looks like you have declared your collectionView as Optional (with ?) at the end and maybe linked it to your storyboard using an #IBOutlet. To fix your issue you should remove the ? from the :
#IBOutlet var collectionView: UICollectionView?
and substitute it with:
#IBOutlet var collectionView: UICollectionView!
this way you are telling the compiler that your collectionView definitely exists (because you have linked it from your storyboard).
Alternatively, you can bind your collectionView by doing this:
if let collectionView = collectionView {
collectionView.reloadItemsAtIndexPaths(myArrayOfIndexPaths)
}
Swift 4 reload all items in section
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
var indexPaths: [NSIndexPath] = []
for i in 0..<collectionView!.numberOfItems(inSection: 0) {
indexPaths.append(NSIndexPath(item: i, section: 0))
}
collectionView?.reloadItems(at: indexPaths as [IndexPath])
}
Swift 3 version code
var indexPaths = [IndexPath]()
indexPaths.append(indexPath) //"indexPath" ideally get when tap didSelectItemAt or through long press gesture recogniser.
collectionView.reloadItems(at: indexPaths)
as a beginner i didn't know that there's a diference when you implement a collectionView as a direct child from another viewController and implement it as an embedded view inside a viewController, maybe some dev is facing the same problem and they doesn't know, if so, look for the correct approach :)
If you want to reload for single cell, you can specify the specific of the cell like this :-
for index in indexPaths {
if index == [0, 0] {
print(index)
self.collectionView?.reloadItems(at: [index])
}
}
Hope this helps.

how to programmatically deselect a static cell iOS

I have a static cell and when clicked it launches a modal view. Except when i return from the modal view the cell is still selected? Why is it doing this and how can I make it only make the cell selected until the modal completely covers the view.
Thanks in advance
For Swift 2 :
override func viewWillAppear(animated: Bool) {
if let indexPath = self.tableView.indexPathForSelectedRow {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
For Swift 3 and 4 :
override func viewWillAppear(animated: Bool) {
if let indexPath = self.tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
}
If you use a UITableViewController instead of a UIViewController this will be done automatically. Otherwise, you need to do the deselecting yourself using
deselectRowAtIndexPath:animated: on the UITableView. The best place to do this is probably on viewDidAppear: of the presenting view controller. That way, the user still sees the deselecting animation allowing them to reorient themselves.
If you don't need to track the selected row for other purposes, you can use
indexPathForSelectedRow to determine which index path needs to be deselected (if any).
I know this is too late but may help someone who's using Swift -
This will give a nice effect when you return to masterViewController from detailViewController
override func viewWillAppear(animated: Bool)
{
if let indexPath = self.tableView.indexPathForSelectedRow
{
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
Otherwise add this delegate method of TableView and call it from didSelectRowAtIndexPath
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
{
if let indexPath = tableView.indexPathForSelectedRow
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
You can use this in your UITableViewController;
- (void)viewWillAppear:(BOOL)animated
{
[yourTableView deselectRowAtIndexPath:[yourTableView indexPathForSelectedRow] animated:YES];
}
In your condition, yourTableView property should be self.tableView
swift 3
override func viewWillAppear(animated: Bool) {
tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true)
}

Swift/parse : IBAction in a tableViewCell to make pfrelation

I would like to implement an IBAction in a tableViewCell.
The tableView dysplay a list of users, and for each user, i have a follow button.
The follow method needs to specify the user to follow witch is the cell's user.
How could i do this ?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: AddFirendsTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)as AddFirendsTableViewCell
let users:PFObject = self.userList.objectAtIndex(indexPath.row) as PFObject
return cell
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let users:PFObject = self.userList.objectAtIndex(indexPath.row) as PFObject
}
#IBAction func followUnfollow(sender: AnyObject) {
//what should i put here ?
}
func follow (user :PFUser) {
var relation : PFRelation = PFUser.currentUser().relationForKey("KfriendsRelation")
relation.addObject(user)
PFUser.currentUser().saveInBackgroundWithBlock { (succeed:Bool, error: NSError!) -> Void in
if error != nil {
println(error)
}
}
}
I'm a little confused on how you are going about it, though I will just run with it for a moment. In order to get a reference to a cell, you could use the following line:
let indexPath = tableView.indexPathForRowAtPoint(buttonPosition)
You could then use the index path to get a reference to the cell, and therefore do what you like with it:
let cell = tableView.cellForRowAtIndexPath(indexPath)
However, i would personally move the IBAction into the cells subclass of AddFirendsTableViewCell where you already have access to all of the cells variables and values. This then means that every cell will work the same and you won't have to do some weird work-around to try and find out which button got pressed. But, that is just my opinion. Hopefully this will have helped.

Why does UITableViewCell remain highlighted?

What would cause a table view cell to remain highlighted after being touched? I click the cell and can see it stays highlighted as a detail view is pushed. Once the detail view is popped, the cell is still highlighted.
In your didSelectRowAtIndexPath you need to call deselectRowAtIndexPath to deselect the cell.
So whatever else you are doing in didSelectRowAtIndexPath you just have it call deselectRowAtIndexPath as well.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Do some stuff when the row is selected
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
The most clean way to do it is on viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Unselect the selected row if any
NSIndexPath* selection = [self.tableView indexPathForSelectedRow];
if (selection) {
[self.tableView deselectRowAtIndexPath:selection animated:YES];
}
}
This way you have the animation of fading out the selection when you return to the controller, as it should be.
Taken from http://forums.macrumors.com/showthread.php?t=577677
Swift version
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// deselect the selected row if any
let selectedRow: IndexPath? = tableView.indexPathForSelectedRow
if let selectedRowNotNill = selectedRow {
tableView.deselectRow(at: selectedRowNotNill, animated: true)
}
}
For the Swift users, add this to your code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
It's paulthenerd's answer but in Swift instead of Obj-C.
Did you subclass -(void)viewWillAppear:(BOOL)animated? The selected UITableViewCell won't deselect when you don't call [super viewWillAppear:animated]; in your custom method.
Swift 3 Solution
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
If you are using a UITableViewCell, then comment the following line
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// [super setSelected:selected animated:animated];
}
Hope this helps.
Updated with Swift 4
After few experiments, also based of previous answers, I've got the conclusion that the best behaviour can be achieved in 2 ways: (almost identical in practice)
// First Solution: delegate of the table View
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
}
// Second Solution: With the life cycle of the view.
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
let selectedRow: IndexPath? = tableView.indexPathForSelectedRow
if let selectedRow = selectedRow {
tableView.deselectRow(at: selectedRow, animated: false)
}
}
I'm personally adopting the first solution, because it's simply more concise. Another possibility, if you need a little animation when you return to your tableView, is to use viewWillAppear:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let selectedRow: IndexPath? = _view.tableView.indexPathForSelectedRow
if let selectedRow = selectedRow {
_view.tableView.deselectRow(at: selectedRow, animated: true)
}
}
Last but not least, if you're using a UITableViewController, you can also take advantage of the property clearsSelectionOnViewWillAppear.
To get the behaviour Kendall Helmstetter Gelner describes in his comment, you likely don't want deselectRowAtIndexPath but rather the clearsSelectionOnViewWillAppear property on your controller. Perhaps this was set to YES by accident?
See the comment in the default Apple template for new UITableViewController subclasses:
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
}
Swift 5 Solution:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
I was getting this problem as well for my drill-down application. After a viewcontroller, which I'll call VC, returns after pushing another ViewController, the selected cell in VC remained highlighted. In my app, I had created VC to handle the second level (out of three levels) of my drill-down.
The problem in my case is that VC was a UIViewController (that contained a View that contained a TableView). I instead made VC a UITableViewController (that contained a TableView). The UITableViewController class automatically handles the de-highlighting of the table cell after returning from a push. The second answer to the post "Issue with deselectRowAtIndexPath in tableView" gives a more complete answer to this problem.
The problem did not occur for the root viewcontroller because when I created the app as a "Navigation-based App" in XCode, the resulting root viewcontroller was already made to subclass UITableViewController.
If none of these work for you, consider this work-around:
Use an unwind segue to call:
#IBAction func unwind_ToTableVC (segue: UIStoryboardSegue) {
if let index = tableView.indexPathForSelectedRow {
tableView.deselectRowAtIndexPath(index, animated: true)
}
}
Why do this? Primarily if you're having trouble getting the deselect code to run at the right time. I had trouble with it not working on the viewWillAppear so the unwind worked a lot better.
Steps:
Write the unwind segue (or paste from above) into your 1st VC (the one with the table)
Go to the 2nd VC. Control-drag from the Cancel/Done/Etc button you're using to dismiss that VC and drag to the Exit Icon at the top.
Select the unwind segue you created in step 1
Good luck.
I am using CoreData so the code that worked for me was a combination of ideas from various answers, in Swift:
override func viewDidAppear(_ animated: Bool) {
if let testSelected = yourTable.indexPathForSelectedRow {
yourTable.deselectRow(at: testSelected, animated: true)
}
super.viewDidAppear(true)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
I've been having the same issue for long time so in case anyone else is struggling:
Take a look at your -tableView: cellForRowAtIndexPath: and see if you are creating cells or using a 'reuse identifier'. If the latter, make sure that your table in IB has a cell with that identifier. If you're not using a reuse Identifier just create a new cell for each row.
This should then give your table the expected 'fade selected row' on appearing.
Use this method in UITableViewCell class
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
// Just comment This line of code
// [super setSelected:selected animated:animated];
}
For Swift 3:
I would prefer it to use in viewDidDisappear
Define:-
var selectedIndexPath = IndexPath()
In viewDidDisappear:-
override func viewDidDisappear(_ animated: Bool) {
yourTableView.deselectRow(at: selectedIndexPath, animated: true)
}
In didSelectRowAtIndexPath:-
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
selectedIndexPath = indexPath
}
if the cell is remaining highlighted after touching it, you can call UITabelView method,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
`[tableView deselectRowAtIndexPath:indexPath animated:YES];`
}
Or, you can use the following method and modify it according to your requirements,
// MARK: UITableViewDelegate
func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.backgroundColor = UIColor.greenColor()
}
}
func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.backgroundColor = UIColor.blackColor()
}
}
Xcode 10, Swift 4
I had this same issue and discovered I left an empty call to viewWillAppear at the bottom of my tableViewController. Once I removed the empty override function the row no longer stayed highlighted upon return to the tableView view.
problem func
override func viewWillAppear(_ animated: Bool) {
// need to remove this function if not being used.
}
removing empty function solved my problem.

Resources