2015-02-07 64 views
0

我试图插入单个自定义图像作为UITableView的单元的accessoryView。然而,我有一个有趣的效果:首先,图标插入任意两个单元格,然后当我向上滚动时,所有图标消失,图像只显示在底部单元格中,一旦下一个图标滚动就消失。这是我使用的代码:当在iOS上滚动UITableView时,配件图像消失

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; 

而在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; 
} 

我也试图懒洋洋地分配accessoryView的,思考的问题可能已经与重复分配,但如果我在所有单元格中使用相同的图像,则图像不会完全显示。

这是怎么回事显示,图标只是在最后一个单元格: Table view with funny accessory view effect

我也安装了它在其他的tableView有,出于某种原因,子类的layoutSubview不叫和accessoryViews会定期出现在所有单元上,尽管尺寸不正确。因此,问题似乎在layoutSubview回调中休息,即使我忽略它可能是什么。 但是,两种情况下的问题都是在触摸图像时不调用accessoryButtonTappedForRowWithIndexPath回调,而是调用didSelectRowAtIndexPath回调。

回答

0

实际上,当我拨出layoutSubviews回调(通过手动调整图像大小),所有图像即使在原始表格上也能正确显示。唯一持久的问题是,点击图像时不会调用accessoryButtonTappedForRowWithIndexPath。

0

我发现这个解决方案也解决了附件回调问题: Using a custom image for a UITableViewCell's accessoryView and having it respond to UITableViewDelegate

我其实有块管理的问题,以便通过一些内部参数。这是最终的解决方案:

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