我正在尝试使用 UIButton
执行转场,它位于名为 GFHomeCell
的自定义 UITableViewCell
类中.
I'm trying to perform a segue using a UIButton
, which is located within in a custom UITableViewCell
class called GFHomeCell
.
GFHomeCell
有一个 postID
属性,我想将其发送到为 segue 做准备.我设置了一个在按下按钮时运行的方法;但是,在按钮按下的方法中,我需要发送者是 GFHomeCell
(或者至少我假设是这样).
The GFHomeCell
has a postID
property, which I want to send in the prepare for segue. I set up a method to run when the button is pressed; however, in the button-pressed method, I need the sender to be a GFHomeCell
(or at least that's what I assume).
有人知道我该怎么做吗?这是我的代码
Does anyone have any ideas how I can do that? Here is my code
我的cellForRowAtIndexPath
:
GFHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsfeedCell" forIndexPath:indexPath];
NSDictionary *rootObject = self.posts[indexPath.row];
NSDictionary *post = rootObject[@"post"];
NSDictionary *group = post[@"group"];
NSString *groupName = group[@"name"];
cell.actionLabel.text = [NSString stringWithFormat:@"New post trending on %@", groupName];
cell.descriptionLabel.text = post[@"body"];
cell.descriptionLabel.numberOfLines = 0;
cell.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.likesLabel.text = [NSString stringWithFormat:@"%@", post[@"likes"]];
cell.postId = post[@"id"];
cell.groupName = group[@"name"];
cell.postBody = post[@"body"];
cell.likeButton.tag = indexPath.row;
[cell.likeButton addTarget:self action:@selector(likeButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[cell.commentButton addTarget:self action:@selector(commentButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
NSString *urlString = [NSString stringWithFormat:@"%s/%@", kBaseURL, @"images/"];
NSURL *url = [NSURL URLWithString:urlString];
[cell.imageView setImageWithURL:url
placeholderImage:[UIImage imageNamed:@"Newsfeed-Image-Placeholder"]];
return cell;
这是单击按钮时我正在运行的方法.我的想法是我需要这里的发件人是一个单元格,而不是一个按钮,因为我在 prepareForSegue
中发送的 postId
属性只存在于 GFHomeCell
:
Here is the method I'm running when the button is clicked. My thought was that I need the sender here to be a cell, not a button, as the postId
property I'm sending in my prepareForSegue
only exists on a GFHomeCell
:
- (void)commentButtonClick:(id)sender {
[self performSegueWithIdentifier:@"addCommentSegue" sender:sender];
}
最后是我的prepareForSegue
(我只包含了与这个segue相关的部分):
Finally my prepareForSegue
(I only included the part relevant to this segue):
} else if ([segue.identifier isEqualToString:@"addCommentSegue"]) {
GFPostShowViewController *destViewController = segue.destinationViewController;
GFHomeCell * cell = sender;
destViewController.postId = [cell.postId copy];
destViewController.groupName = [cell.groupName copy];
destViewController.postBody = [cell.postBody copy];
} else {}
我是 iOS 新手,这让我很困惑,所以非常感谢任何帮助,谢谢.
I'm new to iOS and this has me stumped so any help would be much appreciated, thanks.
对于这种情况基本上有两种常见的方法.一种是向上搜索按钮的超级视图,直到找到单元格.您不应该依赖于上一层或两层,因为层次结构在过去已经改变,并且可能会再次改变(在 iOS 6 中您需要上两层,但在 iOS 7 中需要上三层).你可以这样做,
There are basically two common approaches to this situation. One is to search up through the button's superviews until you find the cell. You shouldn't rely on going up one or two levels, because the hierarchy has changed in the past, and may change again (you need to go up two levels in iOS 6, but 3 in iOS 7). You can do it like this,
-(void)commentButtonClick:(UIButton *) sender {
id superView = sender.superview;
while (superView && ![superView isKindOfClass:[UITableViewCell class]]) {
superView = [superView superview];
}
[self performSegueWithIdentifier:@"addCommentSegue" sender:superView];
}
另一种方法是在 cellForRowAtIndexPath: 中为您的按钮分配一个标签:等于 indexPath.row(如果您只有一个部分),然后使用 sender.tag 获取包含点击按钮的单元格的 indexPath.
The other way is to assign a tag to your button in cellForRowAtIndexPath: equal to the indexPath.row (if you only have one section), and then use sender.tag to get the indexPath of the cell that contained the tapped button.
这篇关于获取 UIButton 的 UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!