首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向MKPlacemark addressDictionary添加UITableViewCell

向MKPlacemark addressDictionary添加UITableViewCell
EN

Stack Overflow用户
提问于 2015-10-29 13:30:21
回答 2查看 653关注 0票数 0

我正试图在我的UITableViewCell's textDetailLabel中添加一个街道地址。我面临的问题是如何为Table View中的每个cell获取唯一的街道地址。

现在,它正在获取字典中第一个实例的第一个街道值,并将其设置为每个cell的街道地址。

我假设我需要捕获我所在的索引,然后从字典中检索街道值,尽管我不完全确定如何这样做。

这是我的密码

代码语言:javascript
复制
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)!
        }


    }

}

街道地址一直在重复

预先感谢您的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-29 14:18:56

您需要创建"placeMarkAddress“数组。

代码语言:javascript
复制
var placeMarkAddress: NSMutableArray;

现在,把地址加进去。

代码语言:javascript
复制
for item in (response?.mapItems)! {
               self.placeMarkAddress.addObject(item.placemark.addressDictionary!)

 }
票数 2
EN

Stack Overflow用户

发布于 2015-10-29 13:50:42

首先,您需要为每个UITableViewCell获取MKmapItem's instance,然后从MKmapItem's plcaemark中找到street address

注意事项:Objective中的代码使其快速

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33415617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档