2016-10-06 7 views
0

tableviewが最初にロードされると、索引print(indexPath.row)に索引0,1,2が表示されます。 tableviewが再びロードされると、indexpath自体が3回繰り返され、デバッガでは0,1,2,0,1,2,0,1,2になります。これは2秒以内に発生しますが、これより前にテーブルビューをスクロールすると致命的なエラーが発生します。Index out of range error。Indexpath.rowリピートが断続的に索引を外す範囲外エラー

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! EstablishmentCell 
    let object = Model.sharedInstance.items[indexPath.row] 


    cell.venueDistance.text = object.venueDistance 


    cell.titleLabel.text = object.name 

    object.loadCoverPhoto { image in 
     dispatch_async(dispatch_get_main_queue()) { 
      cell.coverPhotoView.image = image 
     } 
    } 


      let myCustomSelectionColorView = UIView() 
      myCustomSelectionColorView.backgroundColor = UIColor(red:0.16, green:0.65, blue:0.64, alpha:1.0) 
      cell.selectedBackgroundView = myCustomSelectionColorView 

     } 

}self.activityIndicatorView.stopAnimating() 


    return cell 

}} 

EDIT1: 設立クラス

class Establishment: NSObject { 


//Properties 
var record: CKRecord! 
var name: String! 
var establishmentDescription: String! 
var establishmentOpening: String! 

var venueLocation: CLLocation! 
weak var database: CKDatabase! 
var assetCount = 0 
var establishmentDistance: String! 
let locationManager = CLLocationManager() 


//Map Annotation Properties 
// var coordinate: CLLocationCoordinate2D { 
// return venueLocation.coordinate 
// } 

var title: String? { 
    return name 
} 

var venueDescription: String? { 
    return establishmentDescription 
} 

var venueDistance: String? { 
    return establishmentDistance 
} 


//Initializers 

init(record: CKRecord, database: CKDatabase) { 
    self.record = record 
    self.database = database 

    //let venueLocation = record["Location"] as? CLLocation 



    //let location = CLLocation() 


    //let userLocation = CLLocation(latitude: 51.211963, longitude: -0.769298) 

    self.name = record["Name"] as? String 
    self.establishmentDescription = record["Description"] as? String 
    self.establishmentOpening = record["OpeningTimes"] as? String 
    self.venueLocation = record["Location"] as? CLLocation 



    let location = locationManager.location 

    let distanceBetween: CLLocationDistance = (venueLocation!.distanceFromLocation(location!)) 
    self.establishmentDistance = String(format: "%.f", distanceBetween) 


} 

func fetchPhotos(completion: (assets: [CKRecord]!) ->()) { 
    let predicate = NSPredicate(format: "Establishment == %@", record) 
    let query = CKQuery(recordType: "EstablishmentPhoto", predicate: predicate) 
    // Intermediate Extension Point - with cursors 
    database.performQuery(query, inZoneWithID: nil) { [weak self] results, error in 
     defer { 
      completion(assets: results) 
     } 

     guard error == nil, 
      let results = results else { 
       return 
     } 

     self?.assetCount = results.count 
    } 

} 



func loadCoverPhoto(completion:(photo: UIImage!) ->()) { 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { 
     var image: UIImage! 

     defer { 
      completion(photo: image) 
     } 
     guard let asset = self.record["CoverPhoto"] as? CKAsset, 
      path = asset.fileURL.path, 
      imageData = NSData(contentsOfFile: path) else { 
       return 
     } 
     image = UIImage(data: imageData) 
    } 
} 

モデルクラス:

protocol ModelDelegate { 
func errorUpdating(error:NSError) 
func modelUpdated() class Model{ 

//Properties 
let EstablishmentType = "Establishment" 
static let sharedInstance = Model() 
var delegate: ModelDelegate? 
var items: [Establishment] = [] 
let userInfo: UserInfo 
let locationManager = CLLocationManager() 
var latitude : String! 
var longitude : String! 
var isLoading = Bool() 


//Define Databases 

// Represents the default container specified in the iCloud section of the Capabilities tab for the project. 

let container: CKContainer 
let publicDB: CKDatabase 
let privateDB: CKDatabase 



//Indicator 
// Ask for Authorisation from the User. 
func requestLocation() { 


    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
    self.locationManager.distanceFilter = 10 
    self.locationManager.startUpdatingLocation() 
    // Check if this app has permission to use location services 
    let authorizationStatus:CLAuthorizationStatus = CLLocationManager.authorizationStatus() 

    if authorizationStatus == CLAuthorizationStatus.Denied { 
     // TODO: Tell user that app functionality may be limited 
    } 
    else if authorizationStatus == CLAuthorizationStatus.NotDetermined { 

     // Manually prompt the user for permission 
     self.locationManager.requestWhenInUseAuthorization() 
    } 
    else if authorizationStatus == CLAuthorizationStatus.AuthorizedWhenInUse { 
     self.locationManager.startUpdatingLocation() 
    } 
} 


//Initializers 
init() { 
    container = CKContainer.defaultContainer() 
    publicDB = container.publicCloudDatabase 
    privateDB = container.privateCloudDatabase 

    userInfo = UserInfo(container: container) 
} 

@objc func refresh() { 



    let location = locationManager.location 
    let radius = CGFloat(1000) 


    let predicate = NSPredicate(format: "distanceToLocation:fromLocation:(%K,%@) < %f", "Location", location!, radius) 
    let query = CKQuery(recordType: "Establishment", predicate: predicate) 


    let sort = CKLocationSortDescriptor(key: "Location", relativeLocation: location!) 


    query.sortDescriptors = [sort] 

    publicDB.performQuery(query, inZoneWithID: nil) { [unowned self] results, error in 



     guard error == nil else { 
      dispatch_async(dispatch_get_main_queue()) { 

       self.delegate?.errorUpdating(error!) 
       print("Cloud Query Error - Refresh: \(error)") 
      } 
      return 
     } 
     self.items.removeAll(keepCapacity: true) 

     for record in results! { 
      let establishment = Establishment(record: record, database: self.publicDB) 
      self.items.append(establishment) 
     } 

     dispatch_async(dispatch_get_main_queue()) { 
      self.delegate?.modelUpdated() 
     } 
    } 


} 


func establishment(ref: CKReference) -> Establishment! { 

    let matching = items.filter { $0.record.recordID == ref.recordID } 

    return matching.first 

}} 

答えて

0

あなたのコードに追加され、ここですべてのデータソースの方法を述べます。

そしてまたあなたの次の方法を確認します

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return Model.sharedInstance.items.count 
    } 
+0

ちょうどデータソースを追加しました。私はすでにModel.sharedInstance.items.countを返す –

関連する問題