如何在Swift 3中使用相同的表格查看多个文本字段

时间:2017-09-13 16:45:37

标签: uitableview swift3

嘿大家我正在使用文本字段进行搜索。当我搜索某些内容时,会在searchresultvc上列出数据。在这里如果我点击任何一行,显示另一个VC的数据细节。这里的一切都还可以。问题在此之后开始。如果我在searchresultvc上搜索任何内容(还有另一个文本字段),则会再次列出数据。但我不能点击任何一行。我收到错误:'致命错误:索引超出范围'。(我怎样才能做到极致且至少有代码)

我的ViewController:

@IBOutlet weak var searchTextField: UITextField!
var cName = [String]()
var kTitle = [String]()
var cId = [Int]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func searchButton(_ sender: Any) {
    let headers: HTTPHeaders = [
        "Authorization": "Basic ...",
        "content-type": "application/json"
    ]

    let encodedSearchText = searchTextField.text?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!

    if let searchText = encodedSearchText{

        Alamofire.request("..../api/GetSearchResult?searchText=\(searchText)", headers: headers).responseJSON { (response) in
            if response.result.value != nil{
                let swiftyJsonVar = JSON(response.result.value!)

                for i in 0...swiftyJsonVar.count{
                    self.cName.append(String(describing: swiftyJsonVar[i]["..."]))
                    self.kTitle.append(String(describing: swiftyJsonVar[i]["..."]))
                    self.cId.append(swiftyJsonVar[i]["contentId"].intValue)
                }

                let srvc = self.storyboard?.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultViewController
                srvc.crName = self.cName
                srvc.krTitle = self.kTitle
                srvc.ctId = self.cId
                self.present(srvc, animated: true, completion: nil)
            }

        }
    }
}

我的SearchResult视图控制器:

@IBOutlet weak var searchTextField: UITextField!
@IBOutlet weak var tableView: UITableView!

var crName = [String]()
var krTitle = [String]()
var ctId = [Int]()
var krDetail = String()
var krSource = String()

@IBOutlet weak var dataCount: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    dataCount.text = String(crName.count)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return crName.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "resultCell", for: indexPath) as! SearchResultCellTableViewCell

    cell.kr.text = krTitle[indexPath.row]
    cell.cr.text = crName[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let headers: HTTPHeaders = [
        "Authorization": "Basic ...",
        "content-type": "application/json"
    ]
    Alamofire.request(".../api/GetResultById?contentId=\(contentId[indexPath.row])", headers: headers).responseJSON { (response) in
        if response.result.value != nil{
            let swiftyJsonVar = JSON(response.result.value!)

            self.krDetail = String(describing: swiftyJsonVar[0]["..."]).stringByDecodingHTMLEntities
            self.krSource = String(describing: swiftyJsonVar[0]["..."]).stringByDecodingHTMLEntities
            let resultVC = self.storyboard?.instantiateViewController(withIdentifier: "ResultVC") as! ContentResultViewController
            resultVC.course = self.crName[indexPath.row]
            resultVC.content = self.krTitle[indexPath.row]
            resultVC.cDesc = self.krDetail.removeHtmlFromString(inPutString: self.krDetail)
            resultVC.cSource = self.krSource.removeHtmlFromString(inPutString: self.krSource)
            self.present(resultVC, animated: true, completion: nil)
        }
    }
}


@IBAction func search(_ sender: Any) {
    let headers: HTTPHeaders = [
        "Authorization": "Basic ...",
        "content-type": "application/json"
    ]

    let encodedSearchText = searchTextField.text?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!

    if let searchText = encodedSearchText{

        Alamofire.request(".../api/GetSearchResult?searchText=\(searchText)", headers: headers).responseJSON { (response) in
            if response.result.value != nil{
                let swiftyJsonVar = JSON(response.result.value!)
                self.crName = []
                self.krTitle = []
                for i in 0...swiftyJsonVar.count{
                    self.crName.append(String(describing: swiftyJsonVar[i]["...."]))
                    self.krTitle.append(String(describing: swiftyJsonVar[i]["..."]))
                }

                let srvc = self.storyboard?.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultViewController
                srvc.crName = self.crName
                srvc.krTitle = self.krTitle
                self.tableView.reloadData()

                self.present(srvc, animated: false, completion: nil)
            }

        }
    }
}

0 个答案:

没有答案