Reputation: 555
so I am working on a Swift application that makes use of an auto complete text field that displays a UITableView and filters the results based on what is being typed.
There is a set array with choices that the textfield filters based off whats being typed and displays the results in the table view. Now I need to get the ACTUAL arrays index value when the row is selected, but all I know how to get with the function is the index value of whats currently being displayed. For example, if there are 2 results, i only get the index of 0 and 1, even though the actual array may be 46 and 59.
Is there any way to do this? Alternatively, can I set up an array with the names as strings, and set an int to each one, and use the row selection to get the int then find the name?
Right now this is the code used to filter the array depending on whats typed in the text field
function grabs text as its entered in field
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
autocompleteTableView.hidden = false
var substring :NSString = textField.text
substring = substring.stringByReplacingCharactersInRange(range, withString: String())
return true
}
function goes through the array checking if the strings are in range of whats being typed
@IBAction func searchAutocompleteBrands()
{
for i in 0..<brandNames.count
{
var substring: NSString = brandNames[i] as NSString
if let temp = substring.lowercaseString.rangeOfString(txtBrand.text)
{
filteredArrayResults.append(substring)
}
autocompleteTableView.reloadData()
}
}
function then displays the filtered array results in the tableview
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: UITableViewCell = UITableViewCell()
cell.textLabel?.text = filteredArrayResults[indexPath.row]
return cell
}
From here I need whatever row is selected to then match up the index value to another array containing an "id" and used that in a function to grab all products under the brand that was selected.
Any ideas?
Upvotes: 1
Views: 1883
Reputation: 23078
One possible solution would be to create a struct which holds the text and the brandId:
struct FilterResult {
var name: String
var brandId: Int
}
And then fill the filteredArrayResults
with that struct:
for i in 0..<brandNames.count
{
var substring: NSString = brandNames[i] as NSString
if let temp = substring.lowercaseString.rangeOfString(txtBrand.text)
{
filteredArrayResults.append(FilterResult(name: substring, brandId: i))
}
autocompleteTableView.reloadData()
}
}
Then, in your cellForRowAtIndexPath
you have access to the original index and you can store it in the cell's tag
for further reference:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: UITableViewCell = UITableViewCell()
let filterResult = filteredArrayResults[indexPath.row] as FilterResult
cell.textLabel?.text = filterResult.name
cell.tag = filterResult.brandId
return cell
}
(This is right off my head, untestet. There may be some issues with optionals, though)
Upvotes: 1