点击与其关联的表格行时如何选择地图图钉?

2023-12-31

这里我有2个看法:

  • 墙视图控制器
  • 表视图控制器

墙视图控制器包含 MKMapView,以及表视图控制器是一个子类PFQueryTableViewController显示与注释固定关联的内容行墙视图控制器. 表视图控制器添加为子视图墙视图控制器,因此这两个视图会显示在一起。现在,当我点击地图上的注释图钉时,表格视图表视图控制器将滚动到与成功选择的注释图钉关联的行。但我想做的是也有一个相反的操作,所以当我点击一行时,与该行关联的注释图钉将被选择,并且其标注也将被显示。

下面我提供了相关代码:

WallViewController.h

@interface WallViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>

@property (nonatomic, strong) IBOutlet MKMapView *mapView;
@end

@protocol WallViewControllerHighlight <NSObject>
- (void)highlightCellForPost:(PAWPost *)post;
- (void)unhighlightCellForPost:(PAWPost *)post;
@end

WallViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableViewController = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];
    [self addChildViewController:self.tableViewController];

    self.tableViewController.view.frame = CGRectMake(6.0f, 215.0f, 308.0f, self.view.bounds.size.height - 215.0f);
    [self.view addSubview:self.tableViewController.view];
    self.wallPostsTableViewController.wallViewController = self;
    ....
    }

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    id<MKAnnotation> annotation = [view annotation];
    if ([annotation isKindOfClass:[PAWPost class]]) {
        PAWPost *post = [view annotation];                        
        [PostsTableViewController highlightCellForPost:post];
    } else if ([annotation isKindOfClass:[MKUserLocation class]]) {
        // Center the map on the user's current location:
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        MKCoordinateRegion newRegion = MKCoordinateRegionMakeWithDistance(appDelegate.currentLocation.coordinate, appDelegate.filterDistance * 2, appDelegate.filterDistance * 2);

        [self.mapView setRegion:newRegion animated:YES];
        self.mapPannedSinceLocationUpdate = NO;
    }
}

TableViewController.h

@interface TableViewController : PFQueryTableViewController <WallViewControllerHighlight>

@property (nonatomic, strong)  PAWWallViewController *wallViewController;
- (void)highlightCellForPost:(PAWPost *)post;
- (void)unhighlightCellForPost:(PAWPost *)post;
@end

TableViewController.m

....
@synthesize wallViewController;
....

- (void)highlightCellForPost:(PAWPost *)post {
    // Find the cell matching this object.
    for (PFObject *object in [self objects]) {
        PAWPost *postFromObject = [[PAWPost alloc] initWithPFObject:object];
        if ([post equalToPost:postFromObject]) {
            // We found the object, scroll to the cell position where this object is.
            NSUInteger index = [[self objects] indexOfObject:object];

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:kTableViewMainSection];
            [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

            return;
        }
    }

    // Don't scroll for posts outside the search radius.
    if ([post.title compare:kWallCantViewPost] != NSOrderedSame) {
        // We couldn't find the post, so scroll down to the load more cell.
        NSUInteger rows = [self.tableView numberOfRowsInSection:kTableViewMainSection];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(rows - 1) inSection:kTableViewMainSection];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

- (void)unhighlightCellForPost:(PAWPost *)post {
    // Deselect the post's row.
    for (PFObject *object in [self objects]) {
        PAWPost *postFromObject = [[PAWPost alloc] initWithPFObject:object];
        if ([post equalToPost:postFromObject]) {
            NSUInteger index = [[self objects] indexOfObject:object];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
            [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

            return;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // call super because we're a custom subclass.
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // highlight annotation pin on map - my problem is here ????

    NSLog(@"tap row --> %i", indexPath.row); //******1*****
    NSLog(@"annotation count --> %i", [self.objects count]); //*****2*****
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *textLabel = (UILabel*) [selectedCell.contentView viewWithTag:kPAWCellTextLabelTag];
    NSLog(@"textlabel at the table row tapped --> %@", textLabel.text); //*****3*****


    for (NSInteger i = 0; i < [self.objects count]; i++) {

        PFObject *object = [self.objects objectAtIndex:i];
        PAWPost *postFromObject = [[PAWPost alloc] initWithPFObject:object];
        NSString *text = postFromObject.title;
        NSLog(@"text retrieved from PAWPost --> %@", text); //*****4*****

        if ([text isEqualToString:textLabel.text]) {
            NSLog(@"found matching at %i", i); //*****5*****
            [wallViewController.mapView selectAnnotation:[[wallViewController.mapView annotations] objectAtIndex:i] animated:YES];
            NSLog(@"wallViewController --> %@", wallViewController); //*****6*****
            NSLog(@"wallViewController mapView --> %@", wallViewController.mapView); //*****7*****
        }

    }
}

我的问题出在最后一个方法上(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath in TableViewController.m。现在我有如您所见的代码,使用:

[wallViewController.mapView selectAnnotation:[[wallViewController.mapView annotations] objectAtIndex:i] animated:YES];

但这似乎不起作用。这里肯定有什么问题,但我不知道是什么。

在这里,我在点击表格第一行后提供了日志输出:

2015-01-28 11:24:31.886 Test[8097:60b] tap row --> 0
2015-01-28 11:24:31.888 Test[8097:60b] annotation count --> 40
2015-01-28 11:24:31.890 Test[8097:60b] textlabel at the table row tapped --> Asddsdtest
2015-01-28 11:24:31.890 Test[8097:60b] text retrieved from PAWPost --> ...(40 texts here)
2015-01-28 11:24:31.897 Test[8097:60b] found matching at 0
2015-01-28 11:24:31.899 Test[8097:60b] wallViewController --> (null)
2015-01-28 11:24:31.900 Test[8097:60b] wallViewController mapView --> (null)

附:我发现这个问题 https://stackoverflow.com/questions/15758068/how-to-select-a-map-pin-programmatically但答案和评论似乎没有为像我这样的初学者提供足够的细节。

添加了附加信息

PAWPost.h

@interface PAWPost : NSObject <MKAnnotation>

//@protocol MKAnnotation <NSObject>

// Center latitude and longitude of the annotion view.
// The implementation of this property must be KVO compliant.
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

// @optional
// Title and subtitle for use by selection UI.
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
// @end

// Other properties:
@property (nonatomic, readonly, strong) PFObject *object;
@property (nonatomic, readonly, strong) PFGeoPoint *geopoint;
@property (nonatomic, readonly, strong) PFUser *user;
@property (nonatomic, assign) BOOL animatesDrop;
@property (nonatomic, readonly) MKPinAnnotationColor pinColor;

// Designated initializer.
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate andTitle:(NSString   *)title andSubtitle:(NSString *)subtitle;
- (id)initWithPFObject:(PFObject *)object;
- (BOOL)equalToPost:(PAWPost *)aPost;

- (void)setTitleAndSubtitleOutsideDistance:(BOOL)outside;

@end

PAWPost.m

@interface PAWPost ()

// Redefine these properties to make them read/write for internal class accesses and mutations.
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@property (nonatomic, strong) PFObject *object;
@property (nonatomic, strong) PFGeoPoint *geopoint;
@property (nonatomic, strong) PFUser *user;
@property (nonatomic, assign) MKPinAnnotationColor pinColor;

@end

@implementation PAWPost

- (id)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate andTitle:(NSString *)aTitle andSubtitle:(NSString *)aSubtitle {
    self = [super init];
    if (self) {
        self.coordinate = aCoordinate;
        self.title = aTitle;
        self.subtitle = aSubtitle;
        self.animatesDrop = NO;
    }
    return self;
}

- (id)initWithPFObject:(PFObject *)anObject {
    self.object = anObject;
    self.geopoint = [anObject objectForKey:kPAWParseLocationKey];
    self.user = [anObject objectForKey:kPAWParseUserKey];

    [anObject fetchIfNeeded]; 
    CLLocationCoordinate2D aCoordinate = CLLocationCoordinate2DMake(self.geopoint.latitude, self.geopoint.longitude);
    NSString *aTitle = [anObject objectForKey:kPAWParseTextKey];
    NSString *aSubtitle = [[anObject objectForKey:kPAWParseUserKey] objectForKey:kPAWParseUsernameKey];

    return [self initWithCoordinate:aCoordinate andTitle:aTitle andSubtitle:aSubtitle];
}

- (BOOL)equalToPost:(PAWPost *)aPost {
    if (aPost == nil) {
        return NO;
   }

    if (aPost.object && self.object) {
        // We have a PFObject inside the PAWPost, use that instead.
        if ([aPost.object.objectId compare:self.object.objectId] != NSOrderedSame) {
            return NO;
        }
        return YES;
    } else {
        // Fallback code:

        if ([aPost.title compare:self.title] != NSOrderedSame ||
        [aPost.subtitle compare:self.subtitle] != NSOrderedSame ||
        aPost.coordinate.latitude != self.coordinate.latitude ||
        aPost.coordinate.longitude != self.coordinate.longitude ) {
            return NO;
        }

        return YES;
    }
}

- (void)setTitleAndSubtitleOutsideDistance:(BOOL)outside {
    if (outside) {
        self.subtitle = nil;
        self.title = kPAWWallCantViewPost;
        self.pinColor = MKPinAnnotationColorRed;
    } else {
        self.title = [self.object objectForKey:kPAWParseTextKey];
        //self.subtitle = [[self.object objectForKey:kPAWParseUserKey] objectForKey:kPAWParseUsernameKey];
        self.subtitle = [NSString stringWithFormat:@"@%@",[[self.object objectForKey:kPAWParseUserKey] objectForKey:kPAWParseUsernameKey]];
        self.pinColor = MKPinAnnotationColorGreen;
    }
}

@end

您可以使用 PFObject.objectId 来查找您想要的注释。

PFObject *selectedObject = [self.objects objectAtIndex:i];

for (id annotation in wallViewController.mapView.annotations)
{
    if ([annotation isKindOfClass:[PAWPost class]])
    {
        PAWPost *post = (PAWPost*)annotation;
        if ([post.object.objectId isEqualToString:selectedObject.objectId])
        {
            [wallViewController.mapView selectAnnotation:annotation  animated:YES];
            break;
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

点击与其关联的表格行时如何选择地图图钉? 的相关文章

  • iOS 应用程序中的谷歌地图实时交通状况?

    如何使用 google 地图 sdk 或 api 在 ios 应用程序中添加当前交通状况 如下图所示 有 javascript api 但我没有找到任何适用于 iOS 应用程序的解决方案 只需在 iOS SDK 中添加一行代码即可启用或禁用
  • SplitViewController 与 TabbarController

    我在我的应用程序中使用分割视图功能 我必须将选项卡栏放在 rootViewController 中 但是 当我在选项卡栏中添加控制器并将它们添加到分割视图中时 它不会分割 它只显示detailViewController 这是应用程序中完成
  • 由于无法确认开发者身份而无法打开

    我在 Mac OSX 中开发了一个应用程序和守护进程 并且我已经在 xcode 中与开发者 ID 签署了应用程序和守护进程源代码 在守护进程中 我已经对每个框架进行了签名并打包 并且我使用软件包安装程序对所有框架进行了 pkg 现在我的应用
  • 是否可以?相机 API ios [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我想在应用程序中实现一项功能 当用户
  • 从 Google/Facebook 帐户重新验证用户身份

    因此 我需要创建一个 REST API 来为 IOS 应用程序提供功能 我们允许用户仅使用普通帐户或使用脸书 谷歌登录 我最近一直在阅读 OAuth 我想我了解在我的情况下如何使用 OAuth 的过程 当用户使用脸书 谷歌登录 在我的应用程
  • iOS 显示 UIImage 全屏并启用缩放(捏合和双击)

    我有一个UIImage从相机捕获UIImagePickerController 现在 在用户单击它之后 我希望它显示全屏 并且能够使用捏合手势进行放大和缩小 还可以使用双击手势来放大特定区域 换句话说 我想模拟ios默认图像浏览器的功能 我
  • 平板电脑在第一次单击时悬停,在第二次单击时单击

    发布这个问题主要是希望证实我对该行为的怀疑 从而为其他程序员记录下来 因为我在网上没有找到任何记录 我正在构建一个网站 其导航栏具有以下属性 水平截面是 ul of li 和一些 li li s 两者都有 A n a 元素带您进入该主题 触
  • 使用 Swift 更改整个应用程序中的 UILabel 文本颜色

    在 Swift 中有什么方法可以在整个应用程序中立即更改 UILabel 的文本颜色属性吗 我尝试过使用外观属性 但这不适用于 UILabel textColor 任何方式或任何同样工作的库 一种方法是使用颜色设置 首先在您的 xcasse
  • 如何在 SKAction 中途反转精灵所遵循的路径方向?

    我有一个 SKSpriteNode 它使用 SKAction 沿着圆形路径移动 create the path our sprite will travel along let circlePath CGPathCreateWithElli
  • 如果您查看内部,nib 文件到底是什么样子的?

    我刚刚学习 nibs 和 swift 并对某些东西感到好奇 我知道 如果您有一个 main storyboard 文件 则首先加载根视图控制器的笔尖 然后加载该视图控制器下可能分层存在的任何视图 但是 我想知道一些事情 当他们说笔尖已 加载
  • 如何在 iPhone 应用程序的 url 中传递字符串值

    NSURLRequest request NSURLRequest requestWithURL NSURL URLWithString http www krsconnect no community api html method ba
  • just_audio 无法在 ios flutter 上工作未处理的异常:(-11800)操作无法完成

    我正在尝试从它自己的存储库运行 just audio 示例项目https github com ryanheise just audio tree master just audio example https github com rya
  • 当 iPhone 设备方向朝上/朝下时,我可以判断它是横向还是纵向吗?

    我得到这个代码 如果设备处于左 右横向或上下颠倒状态 它会旋转并显示另一个视图控制器 但如果它的方向朝上或朝下 那么我如何判断它是横向模式还是纵向模式 因为我只想在它面朝上或朝下以及横向模式下旋转 void viewDidAppear BO
  • 我可以使用 NSDateFormatter 将此日期字符串转换为 NSDate 吗?

    我有这个字符串 2010 08 24T16 00 00 05 00 我想从中提取时间部分 即 16 00 并将其转换为 12 小时等效值 即下午 04 00 我正在尝试使用 NSDateFormatter 来完成此任务 但它不起作用 NSD
  • StoryBoard 2 导航控制器指向相同的视图

    有谁知道为什么这不起作用 这样 只有一个选项卡栏加载视图 另一个选项卡栏则显示黑屏 我在没有导航控制器的情况下尝试过 选项卡栏是正确的 而另一个在运行模式下不显示选项卡栏项目 如果有人感兴趣的话我对这个问题的回答 添加两个视图来启动我们感兴
  • 使用 iOS 设备作为 TCP 客户端 - 无 Bonjour

    我想使用 iOS 设备作为 TCP 客户端 但我找不到可理解的 API 指南 示例来说明如何做到这一点 我试过SimpleNetworkStreams and PictureSharing 但他们都使用 Bonjour 我可以轻松建立 UD
  • insertObject:atIndex 的复杂性:

    复杂度是多少 NSArray insertObject atIndex N 或常数 另外 如何找出各种 Objective C 语句的复杂度 有一个在这里讨论 http www cocoadev com index pl NSMutable
  • Apple Mach-O 链接器错误 armv7s 和 libGoogleAdMobAds.a

    我刚刚升级了我的应用程序以在新的 iPhone5 模拟器上运行 但是当我尝试为我的 iPhone 4S 设备构建它时 我收到此 Apple Mach O Liner 错误 ld 文件是通用的 3片 但不包含 n armv7s 切片 User
  • Objective-C 声明的 @property 属性(非原子、复制、强、弱)

    有人可以向我详细解释一下我何时必须使用每个属性 nonatomic copy strong weak等等 对于声明的属性 并解释每个属性的作用是什么 某种例子也很好 我正在使用ARC 非原子的 Nonatomic https stackov
  • 桌面上的 AVAudioSession?

    在 mac 桌面上 我试图录制系统声音 以及可选的麦克风声音 但一开始我只是录制系统声音 我正在遵循本指南 https www appcoda com ios avfoundation framework tutorial https ww

随机推荐

  • 如何使用Node.js的fs模块和express实现文件下载

    因为文件应该动态生成 也许我应该使用 fs 模块的 writeStream 但我用我糟糕的谷歌搜索找不到任何示例代码 对不起 更具体地说 当有人请求时 我想提供包含我在 MongoDB 中的数据的 CSV 文件或 PDF 文件 任何人 请给
  • BrowseFragment 上每个标头有多个 ListRows - Leanback 库

    I m getting started with Leanback support for our app As per UI requirements I need to add multiple list rows correspond
  • 为什么 PyMongo 3 给出 ServerSelectionTimeoutError?

    我在用着 Python 3 4 2 蒙戈 3 0 2 mongolab 运行 mongod 2 6 9 uWSGI 2 0 10 樱桃Py 3 7 0 nginx 1 6 2 uWSGI启动参数 socket 127 0 0 1 8081
  • 如何计算旋转后 HTML5 画布中的点的位置?

    看到后this http codentronix com 2011 04 27 first experiment with html5 a wireframe cube comment 2290代码 我不知道公式在哪里x 新浪 y 科萨来自
  • gcloudcomputessh 返回权限被拒绝(公钥)

    根据谷歌云文档 https cloud google com compute docs instances connecting to instance gcetools 如果我是具有 计算实例管理员 角色的项目成员 我应该能够使用 gcl
  • 上下文切换会导致 CPU 高吗[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我们正在分析一个性能问题 我们可能最多有500个工作线程 CPU使用率不是很高 上下文切换会导致CPU占用率过高吗 换句话说 由于CP
  • 解压受密码保护的文件

    我正在尝试使用 PowerShell 从 USB 驱动器中受密码保护的 zip 中提取文件 我查了很多方法 但最简单的一种似乎不起作用 7ZipPath C Program Files 7 Zip 7z exe zipFile E pass
  • Vagrant 网络与非 Hostonly 网络发生冲突

    我正在尝试启动并运行一个流浪盒子 但我不断收到网络冲突错误 这个盒子是trusty64的全新下载 我一直在搜索与 vagrant 相关的每个文件和文件夹 试图找到指定 IP 的内容 但我找不到 我在 Windows 7 机器上运行 vagr
  • 如何在 MySQL 中解释带参数的查询

    我有一个疑问 SELECT foo FROM bar WHERE some column 我可以从 MySQL 获取解释计划而不填写参数值吗 只要您只执行 equals 而不是 like 这可能会产生短路影响 只需将其替换为一个值 EXPL
  • 如何在我的 iOS 应用程序中正确实现 Services 类?

    我当前的困惑是 专门为我的 Rails 应用程序的服务调用实现一个模型类 这是场景 我有一个名为Service这是 NSObject 的子类 实现文件定义了一些方法 让我们看看doSignUp 我在用AF网络与 api 进行通信 From
  • Windows Phone 8.1:检查互联网连接

    我如何知道手机是否有互联网连接 无论是 WiFi 还是数据 有时手机会连接到 WiFi 但没有像 HotSpots 这样的互联网连接 所以我想要一个代码来知道手机是否连接到互联网 您可以简单地尝试 if NetworkInformation
  • 使用 Android Studio 创建自定义视图

    我正在尝试在 Android Studio 中创建一个可以将其从右向左拖动的小视图 该视图将有 2 个按钮 当您选择其中之一或按其外部时 小菜单将再次隐藏 我一直在寻找 但没有任何图书馆可以做类似的事情 我也不知道该怎么做 我可以在单独的视
  • 自定义微调器:setSelection 向下滚动

    我有一个自定义微调器 我有一个提示标签位于数组 微调器 的最后一个位置 因此为了显示它 我将选择设置为最后一个位置 如下所示 ArrayAdapter myAdapter new MySpinnerAdapter this R layout
  • RESTful 服务能否根据请求标头返回同一资源的 JSON 和 XML?

    我有一个简单的 RESTful 方法 当前返回对象的 JSON 表示形式 我的问题更多是从架构的角度来看的 而不是完全技术性的 RESTful 服务是否应该设计为同时返回 JSON 和 XML 据我所知 这是一种不好的做法 应该为此定义单独
  • dll大小(调试和发布)

    我在其他讨论中读到 与调试 dll 相比 Release dll 的大小减小了 但为什么我制作的dll大小却相反 Release dll比Debug dll大 会引起问题吗 它不会引起问题 可能是编译器在发布版本中 内联 更多项目并创建更大
  • 淘汰组件或模板性能提升

    我有一个可观察的数组 对于每个数组元素 我生成一些 html 表单 非常扩展 因为可观察数组项是依次具有可观察值的大对象 var records ko observableArray p1 ko observable initProp1 p
  • 什么是 Future 以及如何使用它?

    我收到以下错误 A value of type Future
  • SELECT 符合条件的行前后N行?

    我想要复制的行为就像 grep with A and B旗帜 例如grep A 2 B 2 hello myfile txt会给我所有包含 hello 的行 还有它之前的两行和之后的两行 让我们假设这个表模式 id message 1 On
  • 从不同的文件夹加载 DLL 引用?

    我有一个引用 DLL 的 NET 控制台应用程序 如果 DLL 与 EXE 位于同一文件夹中 它运行正常 但我想将 DLL 放在不同的文件夹中 我怎样才能做到这一点 If the DLL位于子文件夹中 您可以将此文件夹添加到AppDomai
  • 点击与其关联的表格行时如何选择地图图钉?

    这里我有2个看法 墙视图控制器 表视图控制器 墙视图控制器包含 MKMapView 以及表视图控制器是一个子类PFQueryTableViewController显示与注释固定关联的内容行墙视图控制器 表视图控制器添加为子视图墙视图控制器