マスターにはマスター - ディテールのアプリがあり、マスターにはUITableビューがあり、検索バーがあり、クリックすると詳細ビューになります。マスターがクリックされたdidSelectRowAtはsearchBarを使用すると最初の要素(indexPath)を覚えています
検索が使用されるまですべてがうまく行きます。たとえば、「Westminster Savings Credit Union」の検索バーに「We」と入力します。検索では、これは最初の行に表示されているように表示されます。最初の行では、最初の行にある「Annie」に属しています。
私はUIImageにリンクされている "Westminster Savings Credit Union"を示すfilteredData [indexPath.row]を持っているので、画像は "Westminster Savings Credit Union"と正しく表示されますが、urlStringは "Annie "のウェブサイト。私は "ウェストミンスター貯蓄信用組合"のための対応するウェブサイトをどうやって得ることができるか分かりません。
私はdidSelectRowAtとSearchBarのコードを掲載しました。ありがとう。
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath:IndexPath) {
var object2: AnyObject?
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
switch indexPath.row {
case 0...MyVariables.count-1:
do {
if (searchActive == true) {
object2 = filteredData[indexPath.row] as AnyObject
print("filtereddata", filteredData[indexPath.row]) //prints Westminster Savings Credit Union
//MyVariables.urlString = ? //not sure how to assign it to urlString
print("urlString in didselectrow", MyVariables.urlString)
MyVariables.companyImage = UIImage(named: filteredData[indexPath.row])// correct image
}
else
{
MyVariables.urlString = [MyVariables.siteAddresses![indexPath.row]]
print("MyVariablessiteAddresses",[MyVariables.siteAddresses![indexPath.row]])
print("urlString in didselectrow", MyVariables.urlString)
MyVariables.companyImage = UIImage(named: MyVariables.test![indexPath.row])
}
}
default:
do {
MyVariables.urlString = ["https://sunwoodsquare.com/store-directory/"]
MyVariables.companyImage = UIImage(named: MyVariables.test![indexPath.row])
}
}
}
}
public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = (MyVariables.test?.filter { $0.range(of: searchText, options: .caseInsensitive) != nil })!
if(filteredData.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
}
なぜ、選択した行のセルを取得して使用しないのですか? – rmaddy