Uitableview Didselectrow Returns Wrong Row Index Value

UITableView didSelectRow returns wrong row index value

check your delegate name is not didDeselectRowAt it is didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)

let alertController = UIAlertController(title: "Hint", message: "You have selected row \(indexPath.row).", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(alertAction)
present(alertController, animated: true, completion: nil)
}

as per your code follow I created the sample project

Returning wrong row index in UITableView didSelectRowAtIndexPath

Its because of IndexPath.Row + 1.
Why you are using it by the way? any good reason ?
It will lead to crash buddy. And this is what happening

Array indexing starts from 0 and so is of indexPath.row. When you will try to call indexPath.row+1 it will give you crash with error 'index was out of bound of array'

In didSelectRowAtIndexPath change every indexPath.row+1 to indexPath.row. Your problem is solved.

Find the correct row for didSelectRow after moveRowAt

I think you left some important part of your code which is responsible for the mess you are having. Why for instance would you have a switch statement in didSelectRowAt? It should look like the following:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
let ViewController = UIStoryboard(name: "firstSBoard", bundle: nil).instantiateViewController(withIdentifier: "firstTabBar")
ViewController.language = languages[indexPath.row]
self.navigationController?.pushViewController(ViewController, animated: false)
}

As you can see in this case your array is the only source of truth and order should never effect your result.

But if you do really need to track those indices you can simply change your structure to have the following:

var languages = [(name: String, index: Int)]()

then

self.languages = defaults.enumerated.map { (name: $1, index: $0) }

cell.textLabel?.text = self.languages[indexPath.row].name

switch(self.languages[indexPath.row].index){
case 0:

EDIT: Adding the full code to map languages:

func initializelanguageList() {
if let defaults = self.languageStore?.array(forKey: "languages") as? [String] {
self.languages = defaults.enumerated.map { (name: $1, index: $0) }
} else {
let languages = ["Español", "Español", "English", "English", "Deutsch", "Deutsch"]
self.languages = languages.enumerated.map { (name: $1, index: $0) }
self.languageStore?.set(languages, forKey: "languages")
}
}

Or if in any case you need to transform them back you would do:

self.languageStore?.set(self.languages.map { $0.name }, forKey: "languages")

UITableView Row Selection Returns Wrong Index Path Until Scroll

Have you actually checked the indexes for the rows, if they are correct?

You might need to reload your tableView again on viewDidAppear when your slide panel is showing up...cause I think this is what happening on your scroll, and it looks like it fixes your issue.

indexPathForSelectedRow returning wrong index

Try this if you are clicking button in row
SET button tag

cell.button.tag = indexPath.row 
[cell.button addTarget:self action:@selector(dataSaveLounge:) forControlEvents:UIControlEventTouchUpInside];

In your cellForRowAtIndexPath

- (IBAction)dataSaveLounge:(id)sender {
UIButton *pressedButton = (UIButton *)sender;
NSLog(@"Tag of button pressed:%d",pressedButton.tag);
}


Related Topics



Leave a reply



Submit