通过 UITableViewCell Parse 中的 UIButton 传递来自 indexPath.row 的对象
Passing PFObjects from indexPath.row through UIButton in TableViewCell Parse
我有一个社交应用程序。我在 TableViewCell 中有一个 UIButton。这个按钮叫做 commentButton。我想用这个按钮实现的是让用户点击它并转到一个表视图,其中包含 post 的评论。问题是 ----- 我很难尝试通过 segue 在当前 indexPath.row 传递 PFObjects。我试过很多东西。但让我帮助您更好地理解我的问题。
正如您在上面看到的,这是我试图在其上启动转场的评论按钮。 commentButton 的标签为 222。我尝试将按钮的 class 更改为 NSINTEGER:
cellForRowAtIndexPath
CommentButton *commentButton = (CommentButton*) [cell viewWithTag:222];
commentButton.index = indexPath.row;
[commentButton addTarget:self action:@selector(commentSegue) forControlEvents:UIControlEventTouchUpInside];
commentButton.showsTouchWhenHighlighted = YES;
调用 Segue @Selector
-(void) commentSegue
{
NSLog(@"Button TOUCHED!");
[self performSegueWithIdentifier:@"showcomments" sender:self];
}
准备转场
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showcomments"])
{
CommentsViewController * cvc = segue.destinationViewController;
if([sender isKindOfClass:[CommentButton class]]){
CommentButton * button = sender;
_currentPost =[self.objects objectAtIndex:button.index];
cvc.postObject = _currentPost;
}
}
我尝试了很多东西,当前代码崩溃并出现 无法识别的选择器发送到实例 错误。
我也试过了:
if([segue.identifier isEqualToString:@"showcomments"]){
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
PFObject *currentObjects = [self.objects objectAtIndex:indexPath.row];
CommentsViewController * cvc = segue.destinationViewController;
NSLog(@"Segue Objects: %@", currentObjects);
cvc.postObject = currentObjects;
}
但是,当我的 tableView 被填充时,无论我选择哪个单元格,我的 NSLog 都会打印出相同的数据。它无法区分我选择的是哪个单元格。
您正在发送 self
至 performSegueWithIdentifier:sender:
。我假设你的意思是将你的按钮作为发件人发送? (您稍后尝试将 sender
转换为 prepareForSegue:sender:
中的 CommentButton
)尝试更改此设置:
-(void) commentSegue
{
NSLog(@"Button TOUCHED!");
[self performSegueWithIdentifier:@"showcomments" sender:self];
}
为此:
-(void) commentSegueWithButton:(CommentButton *)button
{
NSLog(@"Button TOUCHED!");
[self performSegueWithIdentifier:@"showcomments" sender:button];
}
那么需要修改如下:
[commentButton addTarget:self action:@selector(commentSegue) forControlEvents:UIControlEventTouchUpInside];
变成这样:
[commentButton addTarget:self action:@selector(commentSegueWithButton:) forControlEvents:UIControlEventTouchUpInside];
此外,如果您还没有为所有异常创建断点,请在创建断点的同时创建断点。该断点会告诉您在哪一行出现无法识别的选择器错误。
我有一个社交应用程序。我在 TableViewCell 中有一个 UIButton。这个按钮叫做 commentButton。我想用这个按钮实现的是让用户点击它并转到一个表视图,其中包含 post 的评论。问题是 ----- 我很难尝试通过 segue 在当前 indexPath.row 传递 PFObjects。我试过很多东西。但让我帮助您更好地理解我的问题。
正如您在上面看到的,这是我试图在其上启动转场的评论按钮。 commentButton 的标签为 222。我尝试将按钮的 class 更改为 NSINTEGER:
cellForRowAtIndexPath
CommentButton *commentButton = (CommentButton*) [cell viewWithTag:222];
commentButton.index = indexPath.row;
[commentButton addTarget:self action:@selector(commentSegue) forControlEvents:UIControlEventTouchUpInside];
commentButton.showsTouchWhenHighlighted = YES;
调用 Segue @Selector
-(void) commentSegue
{
NSLog(@"Button TOUCHED!");
[self performSegueWithIdentifier:@"showcomments" sender:self];
}
准备转场
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showcomments"])
{
CommentsViewController * cvc = segue.destinationViewController;
if([sender isKindOfClass:[CommentButton class]]){
CommentButton * button = sender;
_currentPost =[self.objects objectAtIndex:button.index];
cvc.postObject = _currentPost;
}
}
我尝试了很多东西,当前代码崩溃并出现 无法识别的选择器发送到实例 错误。
我也试过了:
if([segue.identifier isEqualToString:@"showcomments"]){
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
PFObject *currentObjects = [self.objects objectAtIndex:indexPath.row];
CommentsViewController * cvc = segue.destinationViewController;
NSLog(@"Segue Objects: %@", currentObjects);
cvc.postObject = currentObjects;
}
但是,当我的 tableView 被填充时,无论我选择哪个单元格,我的 NSLog 都会打印出相同的数据。它无法区分我选择的是哪个单元格。
您正在发送 self
至 performSegueWithIdentifier:sender:
。我假设你的意思是将你的按钮作为发件人发送? (您稍后尝试将 sender
转换为 prepareForSegue:sender:
中的 CommentButton
)尝试更改此设置:
-(void) commentSegue
{
NSLog(@"Button TOUCHED!");
[self performSegueWithIdentifier:@"showcomments" sender:self];
}
为此:
-(void) commentSegueWithButton:(CommentButton *)button
{
NSLog(@"Button TOUCHED!");
[self performSegueWithIdentifier:@"showcomments" sender:button];
}
那么需要修改如下:
[commentButton addTarget:self action:@selector(commentSegue) forControlEvents:UIControlEventTouchUpInside];
变成这样:
[commentButton addTarget:self action:@selector(commentSegueWithButton:) forControlEvents:UIControlEventTouchUpInside];
此外,如果您还没有为所有异常创建断点,请在创建断点的同时创建断点。该断点会告诉您在哪一行出现无法识别的选择器错误。