简体   繁体   English

在iOS上滚动UITableView时,附件图像消失

[英]Accessory image disappears when scrolling UITableView on IOS

I am trying to insert a single custom image as the accessoryView for the cells of a UITableView. 我试图插入一个自定义图像作为UITableView的单元格的annexView。 Yet I have a funny effect: at first the icon is inserted any two cells, then when I scroll up, all of the icons disappear and the image just shows on the bottom cell, to disappear as soon as the next one is shown scrolling. 但是我有一个有趣的效果:首先将图标插入到任意两个单元格中,然后当我向上滚动时,所有图标消失,图像仅显示在底部单元格上,直到下一个滚动显示时消失。 This is the code I am using: 这是我正在使用的代码:

NearCell *cell = [myTableView    dequeueReusableCellWithIdentifier:CellIdentifier];
nearContents* contents=[[self sourceForTableKind:tableView==self.myTableView favorites:NO] objectAtIndex:indexPath.row];
cell.bus.text=contents.bus;
cell.destination.text=contents.destination;
cell.selectionStyle=UITableViewCellAccessoryDetailDisclosureButton;
cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pinPoint"]];
return cell;

While in the UITableViewCell subclass nearCell I have: 在UITableViewCell子类nearCell中,我有:

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect accessoryFrame = self.accessoryView.frame;
    accessoryFrame.size.height = self.frame.size.height;
    accessoryFrame.size.width = self.frame.size.height;
    accessoryFrame.origin.y=self.frame.origin.y+21;
    accessoryFrame.origin.x=self.frame.origin.x+self.frame.size.width- accessoryFrame.size.width-5;
    self.accessoryView.frame = accessoryFrame;
}

I also tried to allocate the accessoryView lazily, thinking the problem could have been with the repeated allocations, but if I use the same image in all the cells the image is not shown altogether. 我还尝试延迟分配附件视图,认为问题可能出在重复分配上,但是如果我在所有单元格中使用相同的图像,则不会完全显示该图像。

That is how the thing shows, the icon is just in the last cell : 事情就是这样显示的,图标就在最后一个单元格中: 带有有趣附件视图效果的表视图 :

I also installed it in another tableView and there, for some reason, the layoutSubview of the subclass is not called and the accessoryViews regularly appears on all cells, albeit at a wrong size. 我也将其安装在另一个tableView中,由于某种原因,该子类的layoutSubview没有被调用,并且attachmentViews会定期出现在所有单元格上,尽管大小不正确。 Thus the problem seems resting in the layoutSubview callback, even if I ignore what it might be. 因此,即使我忽略了可能的问题,问题似乎仍在layoutSubview回调中。 The problem in both cases, though, is that the accessoryButtonTappedForRowWithIndexPath callback is not called upon touching the image, rather the didSelectRowAtIndexPath callback is invoked instead. 但是,在两种情况下的问题都是,触摸图像时未调用annexButtonTappedForRowWithIndexPath回调,而是调用了didSelectRowAtIndexPath回调。

In fact when I dispensed of the layoutSubviews callback (by manually resizing the image), all the images are correctly shown even on the original table. 实际上,当我取消了layoutSubviews回调(通过手动调整图像的大小)时,即使在原始表上也可以正确显示所有图像。 The only persistent problem is that the accessoryButtonTappedForRowWithIndexPath is not called upon clicking the image. 唯一的永久性问题是,单击图像后未调用attachmentButtonTappedForRowWithIndexPath。

I found this solution to also solve the accessory callback problem: Using a custom image for a UITableViewCell's accessoryView and having it respond to UITableViewDelegate 我发现此解决方案还可以解决附件回调问题: 对UITableViewCell的annexView使用自定义图像,并使其响应UITableViewDelegate

I am actually managing the issue with a block in order to pass some internal parameters. 我实际上是用一个块来管理问题,以便传递一些内部参数。 This is the final solution: 这是最终的解决方案:

        nearContents* contents=[[self sourceForTableKind:tableView==self.myTableView favorites:NO] objectAtIndex:indexPath.row];
        cell.bus.text=contents.bus;
        cell.destination.text=contents.destination;
        cell.selectionStyle=UITableViewCellAccessoryDetailDisclosureButton;
        cell.accessoryView=button;
        [self registerButton:button blockForKind:tableView==self.myTableView  favorites:NO];

-(void) registerButton:(UIButton*)button blockForKind:(BOOL)normal favorites:(BOOL)favorites{
    [button addEventHandler:^(id sender, id event) {
        NSSet *touches = [event allTouches];
        UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:self.myTableView];
        NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint: currentTouchPosition];
        if (indexPath != nil)
        {
            [self tableView: self.myTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
        }
    }
forControlEvents:UIControlEventTouchUpInside];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM