如何从另一个ViewController中的信息更改在TableCell上插入的数组?
我需要你的帮助!我不知道如何从另一个ViewController中的信息更改插入到TableCell上的数组。这有点搞砸了,但我会用我的代码向你展示。如何从另一个ViewController中的信息更改在TableCell上插入的数组?
在这里我必须对应于不同类别的优惠券的许多交换机符合一个视图控制器,这是代码:
class FiltersViewController: UIViewController {
@IBOutlet weak var restaurantsSwitch: UISwitch!
@IBOutlet weak var sportsSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func returnHome(_ sender: Any) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "home") as! HomeViewController
self.present(vc, animated: false, completion: nil)
}
@IBAction func restaurants(_ sender: UISwitch) {
if restaurantsSwitch.isOn == true{
tuxtlaSwitch.isOn = false
sevillaSwitch.isOn = false
coapaSwitch.isOn = false
coyoacanSwitch.isOn = false
universidadSwitch.isOn = false
polancoSwitch.isOn = false
}
}
@IBAction func sports(_ sender: UISwitch) {
if sportsSwitch.isOn == true{
tuxtlaSwitch.isOn = false
sevillaSwitch.isOn = false
coapaSwitch.isOn = false
coyoacanSwitch.isOn = false
universidadSwitch.isOn = false
polancoSwitch.isOn = false
}
}
}
我只告诉你在例如两个开关与未填充的目的这与许多代码,但有15个开关。
而在其他的ViewController,其被连接到这其中,HomeViewController,包含来自一个JSON优惠券,并且符合上一个TableViewCell显示的10项的数组中,代码:
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var data : NSArray = []
var mainData : NSArray = []
var couponsImg : [UIImage] = []
var couponsTitle : [String] = []
var couponsDesc : [String] = []
var couponsCat : [String] = []
func getCoupons(){
let miURL = URL(string: RequestConstants.requestUrlBase)
let request = NSMutableURLRequest(url: miURL!)
request.httpMethod = "GET"
if let data = try? Data(contentsOf: miURL! as URL) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary
let parseJSON = json
let object = parseJSON?["object"] as! NSDictionary
let mainCoupon = object["mainCoupon"] as! NSArray
let coupons = object["coupons"] as! NSArray
self.mainData = mainCoupon
self.data = coupons
self.couponImg1 = (mainCoupon[0] as AnyObject).value(forKey: "urlImage") as! String
self.couponImg2 = (mainCoupon[1] as AnyObject).value(forKey: "urlImage") as! String
self.couponTitle1 = (mainCoupon[0] as AnyObject).value(forKey: "nameStore") as! String
self.couponTitle2 = (mainCoupon[1] as AnyObject).value(forKey: "nameStore") as! String
self.couponDesc1 = (mainCoupon[0] as AnyObject).value(forKey: "promoDescription") as! String
self.couponDesc2 = (mainCoupon[1] as AnyObject).value(forKey: "promoDescription") as! String
self.couponCat1 = (mainCoupon[0] as AnyObject).value(forKey: "category") as! String
self.couponCat2 = (mainCoupon[1] as AnyObject).value(forKey: "category") as! String
self.couponsImg = [couponImage1!, couponImage2!, couponImage3!, couponImage4!, couponImage5!, couponImage6!, couponImage7!, couponImage8!, couponImage9!, couponImage10!]
self.couponsTitle = [couponTitle1, couponTitle2, couponTitle3, couponTitle4, couponTitle5, couponTitle6, couponTitle7, couponTitle8, couponTitle9, couponTitle10]
self.couponsDesc = [couponDesc1, couponDesc2, couponDesc3, couponDesc4, couponDesc5, couponDesc6, couponDesc7, couponDesc8, couponDesc9, couponDesc10]
self.couponsCat = [couponCat1, couponCat2, couponCat3, couponCat4, couponCat5, couponCat6, couponCat7, couponCat8, couponCat9, couponCat10]
} catch {
let error = ErrorModel()
error.phrase = "PARSER_ERROR"
error.code = -1
error.desc = "Parser error in get Notifications action"
}
}
}
@IBAction func showFilters(_ sender: Any) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "filters") as! FiltersViewController
self.present(vc, animated: false, completion: nil)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableViewCell
cell.couponImg.image = couponsImg[indexPath.row]
cell.couponTitle.text = couponsTitle[indexPath.row]
cell.couponDescription.text = couponsDesc[indexPath.row]
cell.couponCategory.text = couponsCat[indexPath.row]
return cell
}
(再次,我只向你展示了两个优惠券)。问题是我需要将一些过滤器应用于TableCell上的优惠券。第一次出现视图时,它显示了10张优惠券正确,但是当我去过滤器时,把它们中的一些放在一起,它没有什么区别,我试图使用的方法是这样的,首先有一个在FiltersViewController类的实例:
var filters = FilterViewController()
if filters.isMovingToParentViewController == true {
if filters.restaurantsSwitch.isOn == false {
self.couponsImg.remove(at: 0)
self.couponsImg.remove(at: 1)
self.couponsImg.remove(at: 2)
}
if filters.sportsSwitch.isOn == false {
self.couponsImg.remove(at: 3)
self.couponsImg.remove(at: 4)
self.couponsImg.remove(at: 5)
}
}
在这个例子中吼叫,我想说,如果有餐厅关掉,我要删除的餐厅类别的相应优惠券,并同样用运动开关。但首先,我不知道在哪种方法中包含这个逻辑?而且我也不知道这个说明对我的目的是否正确。有人可以给我一只手吗?
您的逻辑不起作用,因为您正在实例化一个新的FilterViewController,它不同于与您关联的FilterViewController屏幕。
您可以使用委托解决此问题。
首先,创建委托:
protocol FilterDelegate {
func updateTable() }
然后,在你FilterViewController加入这一行:
weak var delegate:FilterDelegate?
您HomeViewController有与此委托符合,所以:
类HomeViewController :FilterDelegate ... {
func updateTable() {
/* GET THE DATA FILTERED HERE */
tableview.reloadData()
}
在你FilterViewController:
@IBAction func returnHome(_ sender: Any) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "home") as! HomeViewController
self.delegate = vc
self.present(vc, animated: false, completion: nil)
delegate?.updateTable()
}
我认为应该工作。
编辑:
另一种方法是创建这两个VCS之间的赛格瑞并通过使用“准备”功能,其过滤器是活动的。然后,您可以在HomeVC中获取这些信息,并根据viewDidLoad函数中的过滤器加载表格。
1 - 创建一个对象过滤器:
class Filters {
var tuxtlaSwitchIsOn: Bool
var sevillaSwitchIsOn: Bool
...
init(tuxtlaSwitchIsOn: Bool, sevillaSwitchIsOn: Bool, ...) {
self.tuxtlaSwitchIsOn = tuxtlaSwitchIsOn
self.sevillaSwitchIsOn = sevillaSwitchIsOn
...
}
}
2 - 你HomeVC
class HomeViewController : ... {
...
var filtersActive: Filters?
...
}
3添加属性过滤器 - 在你FilterViewController实例化一个过滤器对象,指示该过滤器上
4 - 在您的FilterViewController准备时,将过滤器对象传递给HomeVC
5 - 在您的HomeVC中,获取Filter对象并根据它过滤数据。
当然这是你需要的。所以你有一组数组充满了数据,你想对它们应用过滤器。首先,你需要为过滤结果创建另一个数组。这是因为当用户删除过滤器时,您仍然想要显示完整列表。为了简化,假设你只有一个数组Foo: [String]
。所以你需要创建另一个名为FooFiltered: [String]
的数组来保存搜索结果。视图控制器加载后,您可以将其保留为空。
接下来,在您的过滤器部分中,建议使用像this post这样的阵列过滤器技术,但如果您想按照自己的方式进行操作,则可以。因此,您只需从符合特定条件的Foo
阵列中获取元素,并将其复制到FooFiltered
阵列中即可。在这里,让我给你看一个手动过滤的例子
func filter() {
FooFiltered = [String]() //Clean up every time before search
for str in Foo {
if str == "criteria" {
FooFiltered.append(str)
}
}
}
现在你有一个过滤项目的列表。你需要一个标志来告诉表格视图显示哪一组数组。假设你有一个名为showSearchResult
的标志,它最初设置为false。当你做过滤器时,将其设置为true。所以,你的cellForRow
会像
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if showSearchResult {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
cell.textField.text = FooFiltered[indexPath.row]
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
cell.textField.text = Foo[indexPath.row]
return cell
}
}
您还需要这个标志更新到所有的表视图的委托方法,如numberOfRowsInSection
等
最后,这些代码,你的表视图配置为显示完整结果或过滤结果基于该标志,并且您在filter()
函数中设置该标志。最后要做的事情是在过滤完成时要求tableView重新加载数据。因此,像这样修改你的过滤器功能,你应该全部设置。
func filter() {
FooFiltered = [String]() //Clean up every time before search
showSearchResul = true
for str in Foo {
if str == "criteria" {
FooFiltered.append(str)
}
}
self.tableView.reloadData()
}
非常感谢您的解释,它非常明确和有帮助!对此,我真的非常感激! :D –
@ZitaNoriegaEstrada不是问题,开心编码:) –
非常感谢,这是将数据从VC交换到另一个的更好方法! ;) –
非常感谢您为您的最后贡献,它帮助了我很多,现在我已经完成了我的解决方案! :D –