如何填充数组并同时查看其数据 - Swift - IOS9

How to populate array and view it's data at the same time - Swift - IOS9

我正在尝试从在线数据库中检索数据,并且成功了;但是,从数据库中检索数据后,我想将它存储在一个数组中,然后用它的数据填充一个列表视图和一个地图视图,但是有一个问题,我能够加载数据并存储它并查看它,但是问题是,每次应用程序加载时,在我转到另一个场景并返回之前,不会出现任何信息,因为我正在通过 AppDelegate 填充数组。但是,如果我通过 viewdidload 填充它,我会在 table 视图中得到重复项。

这是我的代码:

方法 1,导致重复

StoreViewController.swift

class StoreViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate, StoresModelProtocoal {
    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    drawForm()
    setUpMap()
    self.hideKeyboardWhenTappedAround()
    getCurrentLocation()

    let hideStoreDetail: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.hideStoreDetails))
    Map.addGestureRecognizer(hideStoreDetail)

    //Create a nib for the custom cell and use it in the table
    let nib = UINib(nibName: "CustomStoreCell", bundle: nil)
    StoresListTable.registerNib(nib, forCellReuseIdentifier: "customStoreCell")

    let storesModel = StoresModel()
    storesModel.delegate = self
    storesModel.downloadItems()

}

    func itemsDownloaded(items: NSArray) {
    print("Items downloaded")
    for item in items
    {
        if let s = item as? Store
        {
            print(s.Address)
            Globals.unsortedStoresList += [s]
            Map.addAnnotation(s.Annotation)
            do_table_refresh()
        }
    }
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:CustomStoreCell = self.StoresListTable.dequeueReusableCellWithIdentifier("customStoreCell") as! CustomStoreCell

    let s = Globals.unsortedStoresList[indexPath.row]

    cell.loadItem(s.Name, StoreAddress: s.Address, StoreHoursOfOperation: s.HoursOfOperation, StoreDistanceFromCurrentLocation: String(s.DistanceFromCurrentLocation))

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let s = Globals.unsortedStoresList[indexPath.row]

    print(s.Name)
    print(s.Address)
    print(s.HoursOfOperation)
    print(s.DistanceFromCurrentLocation)

    //print("You selected cell #\(indexPath.row)!")
}

func do_table_refresh()
{
    dispatch_async(dispatch_get_main_queue(), {
        self.StoresListTable.reloadData()
        return
    })
}

我知道这个会重复项目,因为每次加载视图时它都会重新下载所有数据;因此,我尝试寻找一种更好的方法,然后我考虑在我的 AppDelegate 中执行下载过程,然后只编写几个函数从数组中获取数据并显示它,但这里的问题是数据将显示在TableView马上就没有重复,但是一开始不会显示在mapview上运行,而是我必须去另一个场景然后返回才能在地图上显示数据

方法数 2

StoreViewController.swift

class StoreViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    drawForm()
    setUpMap()
    self.hideKeyboardWhenTappedAround()
    getCurrentLocation()

    let hideStoreDetail: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.hideStoreDetails))
    Map.addGestureRecognizer(hideStoreDetail)

    //Create a nib for the custom cell and use it in the table
    let nib = UINib(nibName: "CustomStoreCell", bundle: nil)
    StoresListTable.registerNib(nib, forCellReuseIdentifier: "customStoreCell")

     loadMapAnnotations()

}

    func loadMapAnnotations(){
    for item in Globals.unsortedStoresList
    {
        Map.addAnnotation(item.Annotation)
        do_table_refresh()

    }
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:CustomStoreCell = self.StoresListTable.dequeueReusableCellWithIdentifier("customStoreCell") as! CustomStoreCell

    let s = Globals.unsortedStoresList[indexPath.row]

    cell.loadItem(s.Name, StoreAddress: s.Address, StoreHoursOfOperation: s.HoursOfOperation, StoreDistanceFromCurrentLocation: String(s.DistanceFromCurrentLocation))

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let s = Globals.unsortedStoresList[indexPath.row]

    print(s.Name)
    print(s.Address)
    print(s.HoursOfOperation)
    print(s.DistanceFromCurrentLocation)

    //print("You selected cell #\(indexPath.row)!")
}

func do_table_refresh()
{
    dispatch_async(dispatch_get_main_queue(), {
        self.StoresListTable.reloadData()
        return
    })
}

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate, StoresModelProtocoal {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let storesModel = StoresModel()
    storesModel.delegate = self
    storesModel.downloadItems()

    return true
}

//////////////////////////////////////
//Delegates
//////////////////////////////////////
func itemsDownloaded(items: NSArray) {
    print("Items downloaded")
    for item in items
    {
        if let s = item as? Store
        {
            print(s.Address)
            Globals.unsortedStoresList += [s]
            //Map.addAnnotation(s.Annotation)
        }
    }
}

任何帮助将不胜感激,提前致谢。

我找到了临时解决问题的方法 我修改了 StoreViewController.swift 到这里,如果有人有类似的问题。

class StoreViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate, StoresModelProtocoal {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
drawForm()
setUpMap()
self.hideKeyboardWhenTappedAround()
getCurrentLocation()

let hideStoreDetail: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.hideStoreDetails))
Map.addGestureRecognizer(hideStoreDetail)

//Create a nib for the custom cell and use it in the table
let nib = UINib(nibName: "CustomStoreCell", bundle: nil)
StoresListTable.registerNib(nib, forCellReuseIdentifier: "customStoreCell")

Globals.unsortedStoresList.removeAll()  //I added this line of code to remove the old list
let storesModel = StoresModel()
storesModel.delegate = self
storesModel.downloadItems()

}

func itemsDownloaded(items: NSArray) {
   print("Items downloaded")
   for item in items
   {
       if let s = item as? Store
       {
           print(s.Address)
           Globals.unsortedStoresList += [s]
           Map.addAnnotation(s.Annotation)

       }
    }
    do_table_refresh()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Globals.unsortedStoresList.count
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:CustomStoreCell = self.StoresListTable.dequeueReusableCellWithIdentifier("customStoreCell") as! CustomStoreCell

let s = Globals.unsortedStoresList[indexPath.row]

cell.loadItem(s.Name, StoreAddress: s.Address, StoreHoursOfOperation: s.HoursOfOperation, StoreDistanceFromCurrentLocation: String(s.DistanceFromCurrentLocation))

return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//tableView.deselectRowAtIndexPath(indexPath, animated: true)
let s = Globals.unsortedStoresList[indexPath.row]

print(s.Name)
print(s.Address)
print(s.HoursOfOperation)
print(s.DistanceFromCurrentLocation)

//print("You selected cell #\(indexPath.row)!")
}

func do_table_refresh()
{
    dispatch_async(dispatch_get_main_queue(), {
       self.StoresListTable.reloadData()
       return
    })
 }