2015-03-02 92 views
-1

我的表格視圖中有兩個自定義單元格,可以正確顯示。 MyCustomCellB有一個按鈕,當用戶點擊這個按鈕時,我會以編程方式調用一個segue,這也適用,但會導入錯誤的變量。它需要「屬於」MyCustomCellA型單元的不同數據。但是,如果我第一次選擇單元格,然後點擊按鈕,它會正常工作,不知何故,選擇後它會得到正確的數據。我的cellForRowAtIndexPath:不正確?在表視圖中有兩個自定義UITableViewCell的錯誤indexpath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if ([object[@"num"] isEqualToString:@"one"]) { 

     MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 

     //... cell display 
     cell.acceptLabel.tag = indexPath.row; 
     [cell.acceptLabel addTarget:self action:@selector(didTapBttn:) forControlEvents:UIControlEventTouchUpInside]; 
     return cell; 

    } else { 

     MyCustomTableViewCellB *cellMessage = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath]; 

     //... cell display 

     return cellMessage; 

    } 

} 

- (void)didTapBttn:(id)sender { 

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

} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"info"]) { 
     if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) { 

      NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

      DViewController *datap = [segue destinationViewController]; 

      PFObject *passObj = [self.objects objectAtIndex:indexPath.row]; 

      datap.infoName = passObj[@"infoName"]; 
      datap.infoId = passObj[@"id"]; 

     } 

    if ([[segue identifier] isEqualToString:@"desc"]) { 
     if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) { 

      NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

      DViewController *datap = [segue destinationViewController]; 

      PFObject *passObj = [self.objects objectAtIndex:indexPath.row]; 

      datap.infoN = passObj[@"descName"]; 
      datap.infoId = passObj[@"descId"]; 

     } 
    } 
+0

什麼是對象字典?即:[object [@「num」] – Alex 2015-03-02 20:12:45

+0

@Alex這是一個自定義對象,它包含一些字符串屬性。 – rihe 2015-03-02 20:17:33

+0

您是否期望在表重裝過程中更改其值,以便可以實例化不同的表視圖單元格類型?我覺得這是一個糟糕的代碼設計決定。這個'object [@「num」]'值的目的是什麼? – 2015-03-02 20:22:39

回答

1

可以擴展一些數據(即的NSDictionary)你didTappBttn所以你必須確保它總是包含所需的代碼中的data.Also標籤是不相關的,如果你不理解它。也許數據應該是在indexpath標籤處行。

1

首先,由於除了調用performSegue之外,您不會在按鈕方法中執行任何操作,所以您應該直接從按鈕而不是控制器創建segue,並刪除按鈕的操作方法。然後在prepareForSegue:sender:中,sender參數將是按鈕。您需要獲取包含該按鈕的單元格的indexPath。有多種方法可以做到這一點。一種方法是使用表視圖方法indexPathForRowAtPoint:,並將按鈕的原點(轉換爲表視圖的座標系後)作爲點。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender { 
    if ([[segue identifier] isEqualToString:@"info"]) { 
     if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) { 
      CGPoint p = [sender convertPoint:CGPointZero toView:self.tableView]; 
      NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; 

      DViewController *datap = [segue destinationViewController]; 

      PFObject *passObj = [self.objects objectAtIndex:indexPath.row]; 

      datap.infoName = passObj[@"infoName"]; 
      datap.infoId = passObj[@"id"]; 

     } 

    if ([[segue identifier] isEqualToString:@"desc"]) { 
     if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) { 

      NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

      DViewController *datap = [segue destinationViewController]; 

      PFObject *passObj = [self.objects objectAtIndex:indexPath.row]; 

      datap.infoN = passObj[@"descName"]; 
      datap.infoId = passObj[@"descId"]; 

     } 
    } 

我不知道你是否需要進行同樣的更改爲「遞減」賽格瑞,因爲我不知道,一個是如何被觸發做。

相關問題