【问题标题】:Add row in Table View from Search bar view controller to Other view controller in swift将表格视图中的行从搜索栏视图控制器快速添加到其他视图控制器
【发布时间】:2020-10-08 06:15:12
【问题描述】:

是否可以将来自搜索栏呈现视图控制器的数据添加到区域视图控制器。

当我点击搜索栏时,会出现一个当前视图控制器,然后我选择所需的单元格,它会消失并且数据从当前视图控制器传递到区域视图控制器。

这里是搜索栏显示视图控制器

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


       let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CellForShowAllTVC

        print("Did Select call ***********")


        let storyboard = UIStoryboard(name: "Rider", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "yourzoneVC") as! YourZoneVC

vc.arryOfCityName.append(arryOfCity[indexPath.row]) //Pass array to zone View Controller but its not worked

 self.dismiss(animated: true, completion: nil)



    }

这里是区域视图控制器

  override func viewDidAppear(_ animated: Bool) {

        print(arryOfCityName)

        yourZonTV.reloadData()
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return arryOfCityName.count 

    }
//But its show data coming from search bar

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let storyboard = UIStoryboard(name: "Rider", bundle: nil)

Cell3ForYourZone
        let vc = storyboard.instantiateViewController(withIdentifier: "zoneinmapviewVC") as! ZoneInMapViewVC

        vc.getCityCompare1 = arryOfCityName[indexPath.row] as? String

        navigationController?.pushViewController(vc, animated: true)

    }

【问题讨论】:

  • 第一个 sn-p 完全没有意义。 let cell = ... 毫无意义,vc 被实例化然后被丢弃。

标签: ios swift uitableview uisearchbar


【解决方案1】:

因为当您传递数据时,您创建了一个新数据。 您可以使用委托

并且不要创建新的 viewController

示例代码如下:

protocol AViewControllerDelegate: AnyObject {
  func aViewController(add item: <#YourObject#>)
}

final class AViewController: UIViewController {

  weak var delegate: AViewControllerDelegate?

  lazy var bViewController: BViewController  = {
    // UIStoryboard...
    let controller = BViewController()
    self.delegate = controller
    return controller
  }()

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    delegate?.aViewController(add: <#YourObject#>)
  }
}


final class BViewController: UIViewController {


}
extension BViewController: AViewControllerDelegate {

  func aViewController(add item: <#YourObject#>) {
    // add and reload data
  }
}

【讨论】:

    猜你喜欢
    • 2017-12-28
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    相关资源
    最近更新 更多