使用accessoryButton执行segue

时间:2013-04-01 00:15:39

标签: xcode uitableview

我正在做主人+详细页面。我已成功点击主页面中的单元格行跳转到详细信息页面,使用下面的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"MasterToDetail" sender:indexPath];
}

但是,由于我的母版单元格行也有附件按钮,我需要通过点击使这些按钮能够跳转到详细页面。所以我实现了代码

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"MasterToDetail" sender:self];
}

详细信息显示不正确。它始终显示数据列表中的第一个详细数据。我更改了发件人:self到发件人:indexPath也没有工作。

我使用了来自herehere的stackoverflow答案。我是初学者,所以一些概念仍然没有明确的抱歉。

被修改 - preparesegue for masterviewcontroller.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DetailViewController *detailController =segue.destinationViewController;
    SearchDataDetail *bug = [self.bugs objectAtIndex:self.tableView.indexPathForSelectedRow.row];
    detailController.detailItem = bug;


}

-detailviewcontroller.m

#import "DetailViewController.h"
#import "SearchData.h"
#import "SearchDataDetail.h"

@interface DetailViewController ()
- (void)configureView;
@end

@implementation DetailViewController

- (void)configureView
{
    // Update the user interface for the detail item.
    self.rateView.notSelectedImage = [UIImage imageNamed:@"shockedface2_empty.png"];
    self.rateView.halfSelectedImage = [UIImage imageNamed:@"shockedface2_half.png"];
    self.rateView.fullSelectedImage = [UIImage imageNamed:@"shockedface2_full.png"];
    self.rateView.editable = YES;
    self.rateView.maxRating = 5;
    self.rateView.delegate = self;

    if (self.detailItem) {
        self.titleField.text = self.detailItem.data.title;
        self.rateView.rating = self.detailItem.data.rating;
        self.imageView.image = self.detailItem.fullImage;
    }

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configureView];
}

- (void)viewDidUnload
{
    [self setTitleField:nil];
    [self setRateView:nil];
    [self setImageView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

- (void)rateView:(RateView *)rateView ratingDidChange:(float)rating {
    self.detailItem.data.rating = rating;
}

@end

1 个答案:

答案 0 :(得分:0)

这是由于以下陈述

SearchDataDetail *bug = [self.bugs objectAtIndex:self.tableView.indexPathForSelectedRow.row];

在此self.tableView.indexPathForSelectedRow.row中仅适用于行选择 不适用于accessoryButton tap。

你可以通过两种简单的方式解决它

  1. 在.h文件中声明一个变量来存储indexPath并使用它来传递选定的indexPath。
  2. 在发件人中传递indexPath
  3. 一切顺利......