我试图在我的桌面视图中的自定义UITableCell中的UITextField上调用textFieldShouldReturn。我使用这段代码来获取单元格的索引路径,我在另一篇文章的stackoverflow上找到了这个方法:
NSIndexPath *indexPath = [self.tableView indexPathForCell:(friendTableCell*)[[textField superview] superview]];
当我编译并运行时,我没有任何错误,但是当我选择一个单元格,输入文本,并尝试按键盘返回时,没有任何反应。我已经排除了应该调用的其余代码,因为它相当冗长,并且我在didSelectRowAtIndexPath中进行了测试并且它可以工作。如果有人真的很想看,我可以加入它。我想也许我的代表没有为键盘设置正确,但我在我的标题中包含了UITextFieldDelegate,而且我不确定如何设置键盘的代表,因为我' m访问tableview中的自定义单元格。如果有人知道该做什么,或者另一种方法,我会非常感激。谢谢!
更新 - 完成textFieldShouldReturn方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:(friendTableCell*)[[[textField superview] superview] superview]];
NSString *sectionTitle = [self.usernameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionUser = [self.sortedUsernames objectForKey:sectionTitle];
self.recipient = [sectionUser objectAtIndex:indexPath.row];
friendTableCell *cell = (friendTableCell *)[self.tableView cellForRowAtIndexPath:indexPath];
NSString *sendingUser = self.currentUser.username;
NSString *message = [NSString stringWithFormat:@"from %@", [sendingUser uppercaseString]];
if ([self.locationSwitch isOn])
{
if ([cell.message.text length] > 0)
{
message = [NSString stringWithFormat:@"from %@ %@", [sendingUser uppercaseString],cell.message.text];
}
CLLocation *location = locationManager.location;
if(!location)
{
[cell closeMessage:self];
}
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
CLLocationCoordinate2D coordinate = [location coordinate];
PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude];
PFObject *object = [PFObject objectWithClassName:@"Location"];
[object setObject:geoPoint forKey:@"location"];
[object setObject:self.recipient.objectId forKey:@"recipient"];
[object saveEventually:^(BOOL succeeded, NSError *error) {
if(succeeded){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Your Location was sent!"message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
dispatch_sync(queue, ^{
PFQuery *locationQuery = [PFQuery queryWithClassName:@"Location"];
[locationQuery whereKey:@"recipient" equalTo:self.recipient.objectId];
/****** CHANGE THE getFirstObjectInBackground method so it doesn't take the first each time
or DELETE THE LOCATION after it has been viewed **************/
[locationQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
self.locationId = object.objectId;
NSLog(@"Here is you location ID: %@", self.locationId);
dispatch_sync(queue, ^{
self.data = @{
@"alert":message,
@"locationId":self.locationId
};
PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"objectId" equalTo:self.recipient.objectId];
PFQuery *query = [PFInstallation query];
[query whereKey:@"currentUser" matchesQuery:userQuery];
PFPush *push= [[PFPush alloc]init];
[push setQuery:query];
[push setData:self.data];
[push sendPushInBackground];
});
}
}];
});
}
}];
});
}
else if ([cell.message.text length] > 0)
{
message = [NSString stringWithFormat:@"from %@ %@", [sendingUser uppercaseString],cell.message.text];
PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"objectId" equalTo:self.recipient.objectId];
PFQuery *query = [PFInstallation query];
[query whereKey:@"currentUser" matchesQuery:userQuery];
PFPush *push= [[PFPush alloc]init];
[push setQuery:query];
[push setMessage:message];
[push sendPushInBackground];
}
else
{
PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"objectId" equalTo:self.recipient.objectId];
PFQuery *query = [PFInstallation query];
[query whereKey:@"currentUser" matchesQuery:userQuery];
PFPush *push= [[PFPush alloc]init];
[push setQuery:query];
[push setMessage:message];
[push sendPushInBackground];
}
NSLog(@"Message sent!");
[cell closeMessage:self];
[cell resignFirstResponder];
return YES;
}
答案 0 :(得分:0)
尝试此NSIndexPath *indexPath = [self.tableView indexPathForCell:(friendTableCell*)[[[textField superview] superview] superview]];
,因为容器视图和单元格之间还有另一个滚动视图层。
我也可以看到你写了[cell resignFirstResponder];
而你应该使用[textField resignResponder]
希望这会有效..