2011-07-24 51 views
1

如何在創建單元格後獲取單元格的索引路徑?我需要設置一個uniq cell.tag。任何其他想法我怎麼能做到這一點?獲取UITablieViewCell的索引路徑

- (UITableViewCell *)tableView:(UITableView *)tableView cellForManagedObject:(NSManagedObject *)managedObject 
{ 
    ConditionCell *cell = (ConditionCell *)[tableView dequeueReusableCellWithIdentifier:[ConditionCell reuseIdentifier]]; 

    if (nil == cell) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ConditionCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } else 
    { 
     // Reset Image 
     cell.img.image = nil; 
    } 

的原因是我需要找到細胞在不同的thred

[condition processImageDataWithBlock:^(NSData *imageData) { 
      if (self.view.window) { 
       if (cell.tag == [self.tableView indexPathForCell:cell].row) 
       { 
        NSLog(@"%@",cell.tag); 
        UIImage *image = [UIImage imageWithData:imageData]; 
        [condition writeToFile:imageData]; 
        if (image) 
        { 
         cell.img.image = image; 
         [cell.activityIndicator stopAnimating]; 
         [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[self.tableView indexPathForCell:cell]] withRowAnimation:UITableViewRowAnimationNone]; 
        } 
        [condition release]; 
       } 
      } 
     }]; 
+4

does not this question your own question - [self.tableView indexPathForCell:cell]? –

+0

我願意接受上面的評論作爲答案。 @Tom Fobear。 –

+0

它會的。但它不起作用。它總是返回零。 – Chris

回答

1

問題也許是你的調用它不是在主線程,因爲它是一個UIView - 試試這個:

- (void)someMethodToStartAsync 
{ 
    UITableViewCell *cell = however your getting your cell; 
    NSAssert(cell, @"cell was nil"); 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

    [condition processImageDataWithBlock:^(NSData *imageData) { 
     // do something with indexPath 
    } 
} 

indexPath在塊中使用時是一個副本,所以如果您希望初始化的值反映在塊外部,請聲明它如下所示:

_block NSIndexPath *indexPath = whatever; 
+0

感謝您的提示。我找到了另一個解決方案。由於每個單元格的內容都是獨特的,因此我將其用作標籤。問題是這個表格還沒有被繪製。所以它不可能知道那時的索引路徑。 – Chris

+0

@Chris我只是猜測:-x –

+0

謝謝你的幫助! – Chris