Я добавляю UITapGestureRecognizer в свой cellForRowAtIndexPath. Я делаю это так, что на iPad я могу представить UIPopoverController с того места, где был затронут стол. В каждой пользовательской ячейке у меня есть кнопка с именем commentButton. Это то, что можно коснуться. Приложение использует Parse, и каждая строка является записью из PFObject. Вид таблицы загружается отлично, но при нажатии он возвращает неправильный объект. Я нажимаю на первый элемент и возвращает объект из третьей строки.UITapGestureRecognizer возвращает неправильный индекс CellPath
В 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];
}
}
Зачем вам нужно добавить UITapGestureRecognizer к кнопке? Разве эта кнопка не делает этого? Если вы это сделаете, вам не нужно будет определять, где происходит крана. Кроме того, возможно, вы можете найти locationInView: для tableView. Я подозреваю, что вы ссылаетесь на неправильный взгляд. Вы можете изучить, какой вид '[[[[[UIApplication sharedApplication] delegate] window] rootViewController] view]' is? –
У меня есть это, чтобы получить точку зрения на приложение, которое было затронуто. это представление является основным представлением корневого контроллера. Он работает отлично, он дает мне CGPoint, и я отображаю контроллер popover именно в этот момент. Проблема в том, что он не дает мне правильный объект, а скорее последний объект на экране. – user717452
Да, вы используете 'self.theObject', который будет объектом последней выделенной ячейки. Существует только 1 'self.theObject'. Вы должны запросить модель данных, как только вы получите точку касания. Это, скорее всего, ваша проблема. –