【问题标题】:Adding MKPlacemark addressDictionary to UITableViewCell将 MKPlacemark addressDictionary 添加到 UITableViewCell
【发布时间】:2016-01-29 16:14:15
【问题描述】:

我正在尝试将街道地址添加到我的 UITableViewCell 的 textDetailLabel。我遇到的问题是如何捕获Table View 中每个cell 的唯一街道地址。

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

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

这是我的代码

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)!
        }


    }

}

街道地址不断重复

提前感谢您的帮助。

【问题讨论】:

  • 您可以发布您存储在“placeMarkAddress”中的代码吗?它可能会有所帮助。我认为您只需将“placeMarkAddress”替换为 for 循环或任何迭代代码。
  • 已发布。 placeMarkAddress 已正确声明和初始化,并在控制台中给了我预期的结果(不重复,每个 MKMapItem 的唯一值)我遇到的问题是我将如何捕获每个唯一实例字典并将其分配到detailTextLabel

标签: ios swift uitableview cocoa-touch ios9


【解决方案1】:

您需要创建“placeMarkAddress”数组。

var placeMarkAddress: NSMutableArray;

现在,添加地址。

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

 }

【讨论】:

  • 是的!我创建了一个数组,在初始化字典的同一个 for 循环中,我将用地址列表填充我的数组。只要记住每次调用UISearchResultsUpdating 时都要调用removeAll(keepCapacity: boolean) 方法来重置字典AND 数组。谢谢!
【解决方案2】:

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

注意:Objective-C中的代码,所以要快速

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;

【讨论】:

  • 我已经完成了那部分,它工作正常。字典已填充。问题是当您无法指定 NSIndex 时,如何检索每个唯一 MKMapItem 的信息。
  • @Mihado:您需要像下面这样打印.. 街道、城市、州。对吗?
  • 对于每个单元格都会有 NSIndexPath 所以这里不可能有 UITableViewCell 中没有 NSIndexPath 的情况
  • 我发布了一张图片,向您展示它是如何为每个cell 重复的。 @Paresh,字典没有 objectForKey 方法。什么是等价物?
  • @Mihado :您是否按照答案中的说明进行了更改,因为如果没有,那么结果将与您发送的屏幕截图相同。
猜你喜欢
  • 2011-12-01
  • 2012-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多