온라인 데이터베이스에서 데이터를 검색하려고하는데, 성공적으로 수행합니다. 그러나 데이터베이스에서 데이터를 검색 한 후 배열에 저장하고 listview 및 데이터 뷰를 채우고 싶습니다.하지만 문제가 있습니다. 데이터를로드하고 저장하고 볼 수 있습니다. 그러나 문제는 AppDelegate를 통해 배열을 채우므로 다른 장면으로 이동하여 돌아갈 때까지 앱이로드 될 때마다 정보가 표시되지 않는다는 것입니다. 그러나 viewdidload 통해 채우는 경우 내 테이블보기에서 중복 항목을 가져옵니다. 여기 어레이를 채우고 데이터를 동시에 보는 방법 - Swift - IOS9
내 코드입니다 :접근 번호 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)
}
}
}
어떤 도움을 주시면 감사하겠습니다, 미리 감사드립니다.
FUNC은 (NSArray를 항목) itemsDownloaded? 스토어 { 인쇄 (s.Address) Globals.unsortedStoresList + = [S] //Map.addAnnotation(s.Annotation) } } // 대리자/블록 콜백을 만들고이를 컨트롤러가보기에 할당 표. 이 대리자 내부에서 tableview reload data } – Nick
을 호출하십시오. 아직 익숙하지 않지만 배열에 데이터를 넣으면 새로 고침을하는 것처럼 보입니다. 왜 당신은 데이터를 넣은 후 새로 고침을하지 마십시오. – Joshua
@sticker 제안 해 주셔서 감사합니다. 그러나 어떻게 할 수 있습니까? 또는 적어도 그것에 대해 읽을 리소스를 줘? 감사합니다. . –