UIMapGestureRecognizer返回错误的单元格IndexPath



我正在我的cellForRowAtIndexPath中添加一个UIMapGestureRecognizer。我这样做是为了在iPad上,我可以在表格视图被触摸的地方展示UIPopoverController。在每个自定义单元格中,我都有一个名为commentButton的按钮。这是可以触摸的东西。该应用程序使用Parse,每一行都是来自PFObject的一个条目。表视图加载良好,但单击时会返回错误的对象。我单击第一个项目,它会返回第三行的对象。

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                        object:(PFObject *)object
{
    static NSString *CellIdentifier = @"Cell";
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    self.theObject = object;
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getpoint:)];
    tapped.numberOfTapsRequired = 1;
    [cell.commentButton addGestureRecognizer:tapped];
    [tapped release];
//other code below this point was removed as not relevant
}
-(void) getpoint: (id)senderButton
{
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) senderButton;
    gesture.cancelsTouchesInView = NO;
    self.thePoint = [gesture locationInView: [[[[[UIApplication sharedApplication] delegate] window] rootViewController] view ]];
    [self thisisit];
}
-(void)thisisit {
    Mail *mail = [[Mail alloc]init];
    NSLog(@"Afterclicked cell %@", self.theObject);
    NSString *html = self.theObject[@"Request"];
    NSString *thetitle = [self.theObject[@"Title"] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSString *thedate = self.theObject[@"dateMade"];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMM_dd_yyyy"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDate *theNewDate1 = [dateFormat dateFromString:thedate];
    NSString *theNewDate = [dateFormat stringFromDate:theNewDate1];
    mail.thehtml = html;
    self.nameofhtmlfile = [[[[@"http://www.iprayed4u.net/app/" stringByAppendingString:thetitle] stringByAppendingString:@"_"] stringByAppendingString:theNewDate] stringByAppendingString:@".html"];
    //  Reminder *thereminder = [[Reminder alloc] init];
    //thereminder.thehtml = html;
    //thereminder.thetitle = thetitle;
    //thereminder.thedate = thedate;
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[self] applicationActivities:@[mail]];

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        activityVC.excludedActivityTypes = @[ UIActivityTypePostToWeibo,
                                              UIActivityTypeCopyToPasteboard,
                                              UIActivityTypeAssignToContact,
                                              UIActivityTypeMail,
                                              UIActivityTypePrint
                                              ];
    }
    else {
        activityVC.excludedActivityTypes = @[ UIActivityTypePostToWeibo,
                                              UIActivityTypeCopyToPasteboard,
                                              UIActivityTypeAssignToContact,
                                              UIActivityTypeMail,
                                              UIActivityTypePrint,
                                              UIActivityTypeAirDrop
                                              ];
    }
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:activityVC];
        [aPopover presentPopoverFromRect:CGRectMake(self.thePoint.x, self.thePoint.y - 150, 300, 300) inView:[[[[[UIApplication sharedApplication] delegate] window] rootViewController] view ]permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
    else {
    [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:activityVC animated:YES completion:nil];
    }



}

这里有可重复使用的单元格。

这把一切都搞砸了,因为这样一个细胞可以在很多地方重复使用。

尝试始终在cellForRowAtIndexPath中创建单元格,而不使用可重复使用的单元格。

小心——这会对性能产生一些影响。如果这是一个问题,您可以更深入地了解细节,并保留可重用性以修复其背后的问题。(提示:单元格是可重用的,但手势识别器不是)

最新更新