動的セルを含むテーブルビューがあります。
ユーザーがセル内のカスタム ボタン (アクセサリ ボタン) を押すと、プレビューまたは編集 (またはノルウェー語で Forhåndsvis/Rediger) の 2 つのオプションが表示されます。そのため、ユーザーが選択することで、2 つの異なるビュー (ストーリーボード) を開くことができます。
問題は、正しい indexpath.row を取得する方法です。
私の中で- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UIButton のサブクラスを使用しました。
// create a UIButton (UIButtonTypeCustom)
contactAddButtonType = [UIButton buttonWithType:UIButtonTypeCustom];
contactAddButtonType.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
[contactAddButtonType setTitle:@"Options" forState:UIControlStateNormal];
[contactAddButtonType setBackgroundImage:[[UIImage imageNamed:@"211-action.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
contactAddButtonType.backgroundColor = [UIColor clearColor];
[contactAddButtonType addTarget:self action:@selector(sendMail:) forControlEvents:UIControlEventTouchUpInside];
contactAddButtonType.tag = indexPath.row;
// Add a custom accessibility label to the button because it has no associated text.
[contactAddButtonType setAccessibilityLabel:NSLocalizedString(@"AddContactButton", @"")];
//contactAddButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
[cell.contentView addSubview:contactAddButtonType];
cell.accessoryView = contactAddButtonType;
ユーザーがこのボタンを押すと、IBAction メソッドが起動されます。
- (IBAction)sendMail:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Valg" delegate:self cancelButtonTitle:@"Avbryt" destructiveButtonTitle:nil otherButtonTitles:@"Rediger",@"Forhåndsvis", nil];
//initWithTitle:@"Send epost" delegate:self cancelButtonTitle:@"Avbryt" destructiveButtonTitle:nil otherButtonTitles:@"Send via epostklient",@"Send via Uni24", nil];
[actionSheet showInView:self.view];
}
また、actionSheet のハンドラー:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
if (buttonIndex == 0) {
[self performSegueWithIdentifier:@"rediger" sender:self];
}
if (buttonIndex ==1) {
[self performSegueWithIdentifier:@"forhåndsvis" sender:self];
}
}
}
この小さなクラッカーはprepareForSegue
、正しいデータを正しいビューに渡そうとするメソッドを呼び出します。それは私が望むように動作しますが、常に最初の行からデータを送信するため、正しいデータを渡すことができません。
ユーザーが触れた正しい行を見つける方法の例:
if ([segue.identifier isEqualToString:@"forhåndsvis"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSDictionary *dict = [items objectAtIndex:indexPath.row];
NSLog(@"Setting PersonDetailTVC as a delegate of PersonRoleTVC");
UNIRunReportController *vc = (UNIRunReportController *)[segue destinationViewController];
NSString *ordrenr = [dict objectForKey:@"ID"];
[vc setRapportID:@"10"];
[vc setOrdrenr:ordrenr];
//[vc setIsExcisting:YES];
}