【发布时间】: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