获取UITablieViewCell的indexPath

时间:2011-07-24 18:06:21

标签: iphone uitableview nsindexpath

在创建单元格后,如何获取单元格的索引路径?我需要设置一个uniq cell.tag。还有其他想法我怎么能这样做?

- (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];
                }
            }
        }];

1 个答案:

答案 0 :(得分: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;