마스터에 UITable보기가 있고 검색 막대가있는 마스터 상세 정보 응용 프로그램이 있고 클릭하면 세부 정보보기로 이동합니다. 상세보기로 이동하면 웹보기가 마스터 클릭.didSelectRowAt는 searchBar를 사용할 때 첫 번째 요소 (indexPath)를 기억합니다.
검색이 사용될 때까지 모든 것이 잘됩니다. 예를 들어, "Westminster Savings Credit Union"의 검색 창에 "We"를 입력합니다. 검색은 첫 번째 행에 표시되는 것으로 표시합니다. 초기로드에서는 첫 번째 행에 있던 'Annie'에 속합니다.
UIImage에 링크 된 "Westminster Savings Credit Union"을 보여주는 filteredData [indexPath.row]가 있는데 이미지가 "Westminster Savings Credit Union"으로 올바르게 표시되지만 urlString에는 여전히 "Annie "의 웹 사이트. 나는 "Westminster Savings Credit Union"을 위해 해당 웹 사이트를 어떻게 얻을 수 있는지 잘 모르겠습니다.
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