検索目的とすべてのデータの表示に必要な tableView は 1 つだけです。2つの配列を維持するだけです。最初のファイルには元のデータが含まれ、2 番目のファイルには検索されたデータが含まれます。検索開始時に2番目の配列でtableViewをリロードし、すべてのデータをそこに入れます。このように、ナビゲーションは両方のシナリオで機能します。
迅速:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if isSearch == "true"
{
return self.searchArray.count
}
else
{
return self.arr1.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:UITableViewCell? =
tableView.dequeueReusableCellWithIdentifier("tableCell") as? UITableViewCell
if(cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "tableCell")
cell!.selectionStyle = UITableViewCellSelectionStyle.None
}
if isSearch == "true"
{
var main_string = self.searchArray[indexPath.row]
var attributedString = NSMutableAttributedString(string:main_string)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 0.0/255.0, green: 168.0/255.0, blue: 255.0/255.0, alpha: 1.0) , range: NSMakeRange(0, string_to_color.length))
name.attributedText = attributedString
}
else
{
name.text = self.arr1[indexPath.row] as String
}
return cell!
}