2013-04-02 45 views
2

是否可以更改觸摸屏上的UITableViewCellAccessoryDetailDisclosureButton圖像?更改觸摸屏上的詳細信息披露指示圖像

我想讓我的行在tableView中有一個空的圓圈來代替細節泄露按鈕。當我點擊這個空心圓按鈕時,我想用另一個包含複選標記的圖像來更改空心圓的圖像。然後延遲大約半秒後,我想用-accessoryButtonTappedForRowWithIndexPath執行一個操作。

我該怎麼做?

+0

@GabrielePetronella我已經能夠自定義按鈕,使它有空圈。我跟着這個[鏈接](http://iphonedevsdk.com/forum/iphone-sdk-development/71147-change-disclosure-button.html) –

+0

如果用戶在其他時間點擊別的東西,會發生什麼?「延遲大約一半一秒」? –

+0

@robmayoff什麼都沒有。我想到延遲只是爲了讓用戶有一段時間來識別圖像中的變化。 –

回答

2

那麼首先你必須設置你的細胞的accessoryView的是一個自定義的UIView,大概一個UIButton .. 。

UIImage *uncheckedImage = [UIImage imageNamed:@"Unchecked.png"]; 
UIImage *checkedImage = [UIImage imageNamed:@"Checked.png"]; 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(44.0, 44.0, image.size.width, image.size.height); 
[button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside]; 
[button setImage:uncheckedImage forState:UIControlStateNormal]; 
[button setImage:checkedImage forState:UIControlStateSelected]; 
cell.accessoryView = button; 

在你tapButton:方法要應用到的圖像進行必要的修改和執行accessoryButtonTappedAtIndexPath。我只是避免延遲,或者你可以使用調度計時器...

- (void)tapButton:(UIButton *)button { 
    [button setSelected:!button.selected]; 
    UITableViewCell *cell = [button superview]; 
    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
    [self tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath]; 

} 
1

根據您的評論,您已經創建了自己的按鈕並將其設置爲單元的accessoryView

當您創建按鈕,選定狀態設置它的圖像你對號圖片:

[button setImage:checkmarkImage forState:UIControlStateSelected]; 

當按鈕被點擊,設置它的狀態選擇,以便它會顯示對號:

- (IBAction)buttonWasTapped:(id)sender event:(UIEvent *)event { 
    UIButton *button = sender; 
    button.selected = YES; 

然後,禁用用戶交互,因此延遲期間用戶不能做任何事情:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 

獲取索引路徑包含觸摸的按鈕的細胞:

UITouch *touch = [[event touchesForView:button] anyObject]; 
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint: 
     [touch locationInView:tableView]]; 

最後,調度塊的延遲之後運行。在塊,重新啓用用戶互動,並給自己發送消息包括索引路徑:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), 
     dispatch_get_main_queue(), 
    ^{ 
     [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
     [self accessoryButtonTappedForRowAtIndexPath:indexPath]; 
    }); 
} 
+0

好的解決方案。我會避免觸摸邏輯,因爲你知道按鈕的超級視圖是單元格。然後你可以調用[tableView indexPathForCell:cell]來獲取索引路徑。 –

+0

@BenM你不應該依賴該按鈕作爲單元格的直接子視圖。這是一個不屬於公共API的實現細節。有許多方法可以從按鈕訪問僅使用公共API的單元格。使用'indexPathForRowAtPoint:'是一個簡單而簡單的方法。 –

相關問題