我在cellForRowAtIndexPath中添加了一个UITapGestureRecognizer。我这样做,以便在iPad上,我可以从触摸表视图的地方呈现UIPopoverController。在每个自定义单元格中,我有一个名为commentButton的按钮。这是可以触及的事情。该应用程序使用Parse,每行都是PFObject的一个条目。表视图加载正常,但单击时,它返回错误的对象。我单击第一个项目,它将从第3行返回对象。
在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];
}
}
答案 0 :(得分:0)
这里有可重复使用的单元格。
这会搞砸一切,因为这样一个单元格可以在很多地方重复使用。
尝试始终在 cellForRowAtIndexPath 中创建单元格,而不使用可重复使用的单元格。
小心 - 这会影响性能。如果这是一个问题,您可以深入了解细节,并保留可重复使用的完整性,解决其背后的问题。 (提示:单元格 可重复使用,但手势识别器不可用)