我正试图在我的UITableViewCell's 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)!
}
}
}

街道地址一直在重复
预先感谢您的帮助。
发布于 2015-10-29 14:18:56
您需要创建"placeMarkAddress“数组。
var placeMarkAddress: NSMutableArray;现在,把地址加进去。
for item in (response?.mapItems)! {
self.placeMarkAddress.addObject(item.placemark.addressDictionary!)
}发布于 2015-10-29 13:50:42
首先,您需要为每个UITableViewCell获取MKmapItem's instance,然后从MKmapItem's plcaemark中找到street address。
注意事项:Objective中的代码使其快速
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;https://stackoverflow.com/questions/33415617
复制相似问题