UITableViewCell ID

I have just started programming in Swift for iOS and I want to create an app with a list of tables and a search bar. When you click on a cell, a sound will play. But when I search, I can't just use indexPath.row

it because another song will be played.

Is it possible to give each an UITableViewCell

ID so that I can trigger a sound on the ID of the cell found?

+3


source to share


3 answers


When searching, you should ideally have another array that will store the search results.

Let's say you have 10 cells.

You will have an array of 10 sounds to play based on indexPath.row



And you will also have another searchResultsArray that you use to display the search results.

So, whenever SelectRow is called, use a new array and play sound from that array.

+2


source


The best way to achieve this is to change dataSource

your tableView and call it reloadData

when showing search results.

Then you can always find out which song tableView: didSelectRowAtIndexPath:

matches indexPath.row

(because the dataSource

tableView is updated).



Also, you are better off not using cell.tag

because of the cell reuse strategy.

+1


source


I think you need to understand how to work indexPath.row

in UITableView and UISearchBar before doing anything like this. Usually indexPath.row

used as an identifier. And as you move on, you will find it extremely useful.

And I think you have a misunderstanding, because when you click on this, you should be able to play the sound. This is why we need a data source when we customize the UITableView . These are what we call parallel data structures. This means that the UITableViewCell index matches the index from your data source.

After searching, you will have a different data source. So please be careful. Since you are no longer using the original data source.

0


source







All Articles