获取UITableeViewCell的indexPath

发布于 2024-11-26 08:23:02 字数 1504 浏览 3 评论 0原文

创建单元格后如何获取单元格的索引路径?我需要设置一个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;
    }

原因是我需要在不同的线程中找到单元格

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

How can I get the cell's indexpath after I have created the cell? I need to set a uniq cell.tag. Any other idea how I can do that?

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

The reason is I need to find the cell in a different 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];
                }
            }
        }];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪痕残 2024-12-03 08:23:02

也许问题是你不在主线程上调用它,因为它是一个 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;

Perhaps the problem is your calling it not on the main thread because it is a UIView - try this:

- (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 will be a copy when used in the block, so if you want the value you initialized to be reflected outside of the block declare it like so:

_block NSIndexPath *indexPath = whatever;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文