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

标签 objective-c xcode swift xcode6 ios9

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

这是我的代码:

方法 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
    })
}

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

方法 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
    })
 }

关于objective-c - 如何填充数组并同时查看其数据 - Swift - IOS9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37960020/

相关文章:

iphone - CATextLayer 在 iOS7 中的视频中不可见

xcode - ld : -bundle and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together

ios - View 层次结构没有为带有框架初始值设定项的 init 内部的约束做好准备

ios - 监听队列中请求的最佳服务架构(基于队列的负载均衡)?

ios - SWIFT 使用 Stringsdict 和复数规则

ios - UILabels 将动态信息放入标签中并分别列出每个标签?

iphone - 如何在 iPad 上将 View 置于分组的 UITableViewCell 中?

ios - 切换 tabBarController 索引而不是 segueing 时将数据从一个 ViewController 传递到另一个

ios - 如何在 Xcode6 中创建没有开发者帐户凭据的 IPA

ios - 具有场景委托(delegate) iOS 13 的多个 Storyboard文件