Google Map in tableView - swift

I am facing an issue while displaying google maps in a table view cell with swift. I want to display check In (Latitude and Longitude) based on this I want to display google map in table view. I will latitude and longitude from server if no location available means I will get null. so, In one scenario I am getting correct, but while reloading the top-level I am getting map when and where their latitude and longitude is null also. Please guide me.

A map view is an expensive view to instantiate. Even when using dequeueReusableCellWithIdentifier it will still be resource-heavy with a large number of items.
I believe using a way to generate a static map image from your longitude/latitude combination is your best bet.
You can take a look here and you can see how to easily construct an API call to many popular map providers such as Google for a static image map like for example:
http://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=13&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true
Also worth mentioning that some of those providers might have limitations (Like Google might need an API key in case of high traffic). So choose wisely after some research.

Looks like you are facing two issues
1) Already mentioned one "Map appears even when data is nil"
2) Performance issue (It is not mentioned here though)
The first one is due to dequeueing of cell. When you reload table , the cell will be reused(Not created again).
When you return your cell for row in
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
the cell with map view may get return, that is why you get unwanted map there. You should make it nil to prevent this.
2:
When came to performance, its a not the right approach to show mapview in every cell. An alternate approach is to make image out of your map url
use this
let mapUrl: String = NSURL(string:"YourMapURL")
let mapImage = UIImage.imageWithData(NSData.dataWithContentsOfURL(mapUrl))

let latitude:Double = 17.3850
let longitude:Double = 78.4867
let imageURL = NSURL(string: "http://maps.googleapis.com/maps/api/staticmap?center=\(latitude),\(longitude)&zoom=12&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true")
let imagedData = NSData(contentsOfURL: imageURL!)!
staticMapImgView.image = UIImage(data: imagedData)

Related

"target is not running or doesn't have entitlement" message: is this connected to crashes?

Update: this problem is more focused now, and not on quite the same topic. I've asked this question as a follow-on
ORIGINAL QUESTION:
I am getting a crash on a subclassed WKWebView-provisioned app.
ProcessAssertion::acquireSync Failed to acquire RBS assertion 'ConnectionTerminationWatchdog' for process with PID=87121, error: Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}
The problem is, that I can't tell if this is related or not. The actual error on crash is
Thread 1: EXC_BAD_ACCESS (code=1, address=0xbdb2dfcf0470)
Which I was assuming was something running off the end of an array. This makes some sense: I'm selecting from a table that filters out some entries from the data source; but I've checked that carefully; there is no point when a row index greater than the actual rows is accessed (and yes, I'm accounting for the difference between count and index).
The main change here is that I previously had a UIView that acted as a container for a number of CAShapeLayers. I also wanted to overlay text view, but with the proviso that this be via a WKWebView. With two separate views, I would have to either have the CAShapeLayer objects in front of, or behind the WebView. I was seeking a fix to that.
What I have done is substitute a WKWebView for the original UIView. I can add the CAShapes to it, so it performs the original function. It also can, presumably, display the html. And the original suggestion in this answer to a question I asked is what I am working towards. The idea being that it would allow the effect sought, with shapes in front of or behind the html elements.
But the error is thrown after the DidSelect call on the table:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Tracker.track("getting row \(indexPath.row)")
let ptv = tableView as? NovilloTableView
if ptv!.uiType == .textTable {
let gp = Projects.currentProject?.getPaths(type: PaletteView.getCurrentPane())
GitPaths.currentGitPath = gp![indexPath.row]
NotificationCenter.default.post(name: NNames.updateWebText.nn(), object: nil)
return
}
let svgs = Projects.currentProject!.getPaths(type : PaletteView.getCurrentPane())
var gitPath = svgs[indexPath.row]
var gitPaths = GitPaths.getMediaBoundingBoxes(paths: [gitPath])
guard let pathArrays = gitPath.parseForRegBeziers() else { return }
let rslt = pathArrays.0
let regBeziers = pathArrays.1
gitPath.boundingBox = gitPath.getBoundsParamsForPaths(src: regBeziers.isEmpty ? rslt : regBeziers)
GitPaths.currentGitPath = gitPath
// Tracker.track("sending notification")
NotificationCenter.default.post(name: NNames.updateMedia.nn(), object: nil,
userInfo: ["path" : gitPath])
Tracker.track("completed didSelect")
return
}
In other words, the interaction has ended; but I get this crash, even though I can se that the expected result (the rendering of the shapes into the view) has been completed correctly. It seems to be happening right after everything has completed, and no amount of breakpoints has shown anything else to be happening.
This is confusing to me, and I have hit a limit on what I know how to do to dig further into this. Given the nature of web connections, I've wondered if it's some asynchronous issue that I can't debug sequentially; but that's guesswork without any direct evidence.
I suspect that there is a possible configuration problem with the WebView that shows up once I interact with it, by changing its contents. I'm not actually trying to get it to load anything when it crashes, it's only performing its original function as a container for the CAShapeLayers, so I'm confused.
The main view that contains the WKWebView (which is subclassed, to support a function to determine if it should display the web content, and which I've commented out), is set as the delegate for the WKWebView, and that seems to be fine, though there are no actual protocol functions added to that view, not sure if that matters.
The other detail is that the WebView when it does load web content is only loading local text, and not connected to any services. This testing is happening in the Simulator, and I've come across advice elsewhere to allow for background processes that include enabling Background Fetch, etc., but this has done nothing to change the situation...
EDIT: this is the extent of the configuration of the subclassed WKWebView: maybe this is the issue?
mediaDisplay = NovilloWebView()
mediaPane.addSubview(mediaDisplay)
mediaDisplay.navigationDelegate = self
mediaDisplay.uiDelegate = self
mediaDisplay.backgroundColor = .clear

Accessing a NSTableviews custom Tablecellview

I made a NSTableview and a custom tablecellview-class "KSTablecellview" which defines the cells inside the tableview.
Now I'm trying to access these cells and the elements inside them at a certain time, for example triggered through an users input.
For example I wanna change the text of a textfield in one of my custom cellviews. I tried that:
Outlet_TableView.selectRowIndexes(IndexSet(integer: 0), byExtendingSelection: false) // Selecting row #1
let view = Outlet_TableView.view(atColumn: 0, row: 0, makeIfNecessary: false) as! KSTableCellView // getting cellview, at first row/ first column (-> crashes)
view.myTextField.stringvalue = "Hello World"
This fails giving me "[...] [default] Unable to load Info.plist exceptions (eGPUOverrides)"
I tried some other things and tried to work out the problems reason, but I wasn't able to do it, I'm still a beginner.
Any help would be really great.
I'm using XCode and Swift, trying to build an app for Mac OS.
You should try to avoid updating the content of your cells manually. Instead, you should reload the data from your data source.
func reloadData(forRowIndexes rowIndexes: IndexSet,
columnIndexes: IndexSet)
Which will in turn fetch the data from your data source at the given index/column.
For example, your data source would probably contain "Hello World" at the given index after the user event occurred (provided you updated your data source content), and would thus be used in the dataCellFor: delegate method.
May be you're doing these operations before table is loaded for the first time(eg. inviewDidLoad) or on tableView with numberOfRows = 0.
Until tableView loads that row, it won't create the views inside that row and tableView.view(atColumn:, row:, makeIfNecessary: false) will return nil and force unwrapping this nil will make your application crash.

repeating custom cells label swift with core data

This is a followup question to a question previously asked before.
didselectatrow updating multiple cells below reloaded tableview + counter
So after much reading and experimenting even after I accepted the guys answer I am still having the same issue. I am going to try a much different approach to it after reading around I found use of adding/removing the index path by having a Set <NSIndexPath>() var that I am constantly checking.
After much experimentation I finally got a working check on the Set <NSIndexPath> the problem is for some reason in xcode 7 when I try to create a second array to capture the integer values related to the set xcode freezes up on indexing. Which is altogether a different issue. How do I go about keeping track of not only the indexpath row but also the counter as well? My latest code to insert/remove indexpaths at row is:
if indexCount == 1 || indexCount == 2 {
CountIndex.insert(indexPath)
} else {
CountIndex.remove(indexPath)
}
My latest try was to try creating a dictionary that stores the indexpath + the Counter in a dictionary like so var cardCountindexCount = [NSIndexPath: Int]()
Then setting it in the func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { by cardCountindexCount = [indexPath: cardCount]. However I am lacking any experience on going about this approach as it is all experimental and it seems that what I get when I try to recall this is a similar problem. Instead of actually changing the text label in the didselectatrow I am now trying to change it in cellforrow method and creating a universal variable that can store the data changes.
I am willing to admit I am slightly out of my depths on this problem as I have only ever worked with simple tableviews prior. More guidance would be greatly appreciated!
The way I ended up fixing this was by setting the relationship in core data, then saving the quantity of the card in core data, and finally recalling the object relationship and quantity back into the func tableview willDisplayCell method showing the changes in text in that method.

Text and table view data storage in swift

Could anyone tell me what the way of storing a long text in a swift app is. Let's suppose I have an app that has a table view and when I chose a row I go to a new scene where I have a big page filled by text.
The question is where do I have to store the data of the table's row and the whole text? And how?
Do I have to make a model? Is it just one for both the table and the text? Or more than one. Is there any tutorial that explains this exact situation or close to it?
You could use Parse.com framework to retrieve data from a database. There is a lot of documentation on this.
In this link you can find a tutorial that will explain you how to load data from Parse and show it in your UITableView.
Storing Local (in-memory store):
For storing without a internet connection you could use Core Data Stack with NSInMemoryStoreType as storeType. This tutorial will give you a nice idea on how it works.
You can declare a model like it follows:
struct CoreDataModel {
let name: String
let bundle: NSBundle
init(name: String, bundle: NSBundle)
// other properties & methods
}
And then manage it with:
let model = CoreDataModel(name: "MyModel", bundle: myBundle)
let stack = CoreDataStack(model: model,
storeType: NSInMemoryStoreType,
concurrencyType: .MainQueueConcurrencyType)
// Use context
stack.managedObjectContext

How would I go about limiting a users action utilizing Parse?

This is all purely for educational purposes, to help me get a better understanding of how Parse as well as Swift operates.
I would like to make it so a user is only able to like an item once (not being able to hit a button multiple times), as currently, I'm utilizing an anonymous system with Parse.
Would I essentially use an if method with PFUser.CurrentUser() in the likeButton method to halt a user from hitting like again or would I use NSUserDefaults?
I'm not able to post code currently as I'm not near my laptop, however I could later if it helps. Still curious if I could get some info before that however.
Sample code I found on here from a previous question, which essentially implements the same idea.
#IBAction func likeButton(sender: AnyObject) {
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
let object = objectAtIndexPath(hitIndex)
object.incrementKey("count")
object.saveInBackground()
self.tableView.reloadData()
}
Would I call NSUsersDefaults to stunt the user from hitting it more than once?
Instead of calling saveInBackground(), you'd better call the method saveInBackgroundWithBlock: instead. So the strategy is very simple:
First of all, define a 'busy' flag for the object (For example: savingInBackground) and store it wherever you like ( If you are showing 1 item then simply declare a Bool property / If you are showing a list of Item then declare a Dictionary with format ["objectID/Index": Bool]). This flag should be set to true for the item being saved in the background.
Whenever use taps on a Like button
If current item's savingInBackground flag is true, then do nothing
Else:
Set item's savingInBackground to true
Increase Like count and Call saveInBackgroundWithBlock:
In the completion block of saveInBackgroundWithBlock:, set savingInBackground back to false.
I am on train now so I can't write example code, but I hope that it's clear enough to help you to achieve what you want.