2017-10-05 14 views
0

マスターにはマスター - ディテールのアプリがあり、マスターには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() 
} 
+0

なぜ、選択した行のセルを取得して使用しないのですか? – rmaddy

答えて

1

タイトルが誤解を招くことがあります。 searchBarは最初の行を覚えていません。あなたの実装には問題があります。 if (searchActive == true)の下にMyVariables.urlStringに値を割り当てていないので、割り当てられた最後の値が使用されます。

if (searchActive == true)の値にアクセスする方法については、filteredSiteAddressesアレイを作成する必要があります。配列searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)filteredData配列を更新すると、filteredSiteAddressesも更新する必要があります。 if (searchActive == true)より、

MyVariables.urlString = [MyVariables.filteredSiteAddresses![indexPath.row]] 
関連する問題