Mihado
Mihado

Reputation: 1547

Adding MKPlacemark addressDictionary to UITableViewCell

I'm trying to add a street address to my UITableViewCell's textDetailLabel. The issue i'm having is how to capture the unique street address for each cell in the Table View.

Right now it's grabbing the first street value of the first instance in the dictionary and setting it as the street address for every cell.

I'm assuming I need to capture index i'm at and then retrieving the street value out of the dictionary, although i'm not entirely sure how to go about doing this.

Here's my code

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

    let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell")
    // searchResults is an array of MKMapItems
    cell.textLabel?.text = self.searchResults[indexPath.row].name

    // the line that is causing me trouble 
    cell.detailTextLabel?.text = placeMarkAddress["Street"] as? String ?? ""

    return cell

}


func searchQuery(query: String) {


    // request
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = query
    request.region = mapView.region

    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    // search 
    let search = MKLocalSearch(request: request)
    search.startWithCompletionHandler { (response, error) -> Void in

        UIApplication.sharedApplication().networkActivityIndicatorVisible = false

        if(error != nil) {

            print(error?.localizedDescription)

        } else {

            // storing data about location in dictionary
            for item in (response?.mapItems)! {

                self.placeMarkAddress = item.placemark.addressDictionary!
                // prints expected results in console (not repeating, different for each MKMapItem)
                for (key,value) in self.placeMarkAddress {
                    print("\(key) -> \(value)")
                }
            }

            // storing the array of mkmapitems in the array
            self.searchResults = (response?.mapItems)!
        }


    }

}

Address repeating The street address keeps repeating

Thank you in advance for any assistance.

Upvotes: 0

Views: 728

Answers (2)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Firstly you need to get MKmapItem's instance for every UITableViewCell and then find street address from MKmapItem's plcaemark.

Note : code in Objective-C so make it to swift

MKmapItem *mapItem = self.searchResults[indexPath.row];
NSDictionary *itemAddressDictionary = mapItem.placemark.addressDictionary;
//get required string from itemAddressDictionary and set in cell.detailTextLabel
NSString *strStreet = [itemAddressDictionary objectForKey:@"Street"];
cell.detailTextLabel.text = strStreet;

Upvotes: 0

girish_pro
girish_pro

Reputation: 848

You need to create array of "placeMarkAddress".

var placeMarkAddress: NSMutableArray;

Now, add address into it.

for item in (response?.mapItems)! {
               self.placeMarkAddress.addObject(item.placemark.addressDictionary!)

 }

Upvotes: 2

Related Questions