2015-02-07 62 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]; 
}