簡體   English   中英

如何在tableView中找到tapPath附件按鈕的indexPath

[英]How to find indexPath for tapped accessory button in tableView

我有一個部分和幾行簡單的UITableView 當用戶單擊單元格附件按鈕(與detailsSegue連接時。我想知道它是什么單元格行。所以我可以從我的數組中選擇正確的對象並將其分配給下一個視圖中的變量。

我使用了委托方法tableview:accessoryButtonTappedForRowWithIndexPath:並將indexPath值分配給我的私有屬性myRow 比在prepareForSegue:sender:方法我使用self.myRow.row值從數組中選擇正確的對象。

我的問題是這兩種方法似乎執行順序錯誤。 從NSLog我可以看到prepareForSegue:sender:方法首先被執行,我的委托方法正在改變self.myRow值。

所以prepareForSegue:sender:方法總是將錯誤的對象傳遞給下一個視圖(之前被點擊的視圖)。

對不起,我的英國人。 提前感謝您的幫助。

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    self.myRow = indexPath;
    NSLog(@"tapped button at row: %i",self.myRow.row); 
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"addSegue"]) {
        AddViewController *avc = segue.destinationViewController;
        avc.delegate = self;
    }
    else if ([segue.identifier isEqualToString:@"detailsSegue"]) {
        NSLog(@"Segue row: %i",self.myRow.row);
        Annotation *annotation = [self.viewsList objectAtIndex:self.myRow.row];
        NSLog(@"Segue annotation object: %@",annotation.title);
        DetailsViewController *dvc = segue.destinationViewController;
        dvc.wikiKey = annotation.title;
    }
}

正如您所發現的,系統會在向您發送tableview:accessoryButtonTappedForRowWithIndexPath: message 之前向您prepareForSegue:sender: tableview:accessoryButtonTappedForRowWithIndexPath:消息。

但是 ,當它向您發送prepareForSegue:sender:消息時, sender參數是包含附件視圖的UITableViewCell 您可以使用它來確定點擊了哪一行的附件按鈕:

else if ([segue.identifier isEqualToString:@"detailsSegue"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    Annotation *annotation = [self.viewsList objectAtIndex:indexPath.row];
    DetailsViewController *dvc = segue.destinationViewController;
    dvc.wikiKey = annotation.title;
}

發件人將是配件按鈕,對嗎? 在這種情況下,您應該能夠通過超級視圖查找其包含的單元格,然后獲取該單元格的索引路徑。 我之前使用過這樣的方法來完成第一部分:

+ (UITableViewCell *)findParentCellOfView:(UIView *)view {
  if (view == nil || [view isKindOfClass:[UITableViewCell class]]) {
    return (UITableViewCell *)view;
  }
  return [self findParentCellOfView:[view superview]];
}

//我告訴你一個簡單,更好,更簡單的選擇: -

//解決方案: - 您可以在tableView的這種方法中將標簽分配給附件視圖的按鈕

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // set accessory view
    UIButton *oValidHomeAccessryBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    // set target of accessory view button
    [oValidHomeAccessryBtn addTarget:self action:@selector(AccessoryAction:) forControlEvents:UIControlEventTouchUpInside];

    // set tag of accessory view button to the row of table
    oValidHomeAccessryBtn.tag =indexPath.row;

    // set button to accessory view
    cell.accessoryView = oValidHomeAccessryBtn ;
}

//創建您在選擇器中傳遞的方法並獲取標記值,然后單擊附件視圖的indexPath

- (void)AccessoryAction:(id)sender
{
    NSLog(@"%d",[sender tag]);
}

這是獲取單擊附件視圖的行的indexPat的最簡單方法。

您需要做的是從委托方法以編程方式調用segue 假設您正在使用故事板,則需要取消故事板中的segue和委托方法使用中的鏈接:

[self performSegueWithIdentifier: @"IdentifierNameHere" sender: self];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM