2011-06-11 82 views
6

我試圖實現一個TableView,其中每個單元格都有一個'+'按鈕,就像iPod應用程序將歌曲添加到播放列表時一樣。爲AccessoryType添加「Plus」按鈕TableView

cell.accesoryType只有四個值

UITableViewCellAccessoryCheckmark 

UITableViewCellAccessoryDetailDisclosureButton 

UITableViewCellAccessoryDisclosureIndicator

None 

如何才能做到這一點?我是否需要創建一個自定義按鈕並設置cell.accessoryView還是有另一種方法來執行此操作?

回答

0

如果要創建一個custom cell,然後在其上放置一個button在右上角,就像這裏accessory type出現和你的「+」圖像分配給它的按鈕,這樣你也「看到圖像」,並得到「按鈕動作事件」。

請參閱this自定義uitableviewcells教程。

+5

找到解決方案UIButton * button = [UIButton buttonWithType:UIButtonTypeContactAdd]; [button addTarget:self action:@selector(buttonTapped:event :) forControlEvents:UIControlEventTouchUpInside]; cell.accessoryView = button; 並創建了事件處理程序 - (void)buttonTapped:(id)發件人事件:(id)事件{ } – teo 2011-06-11 08:57:21

12

張志賢的解決方案的偉大工程,但它尚未發佈作爲一個答案,所以我重新張貼它:

UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
[button addTarget:self action:@selector(addbuttonTapped) forControlEvents:UIControlEventTouchUpInside]; 
cell.accessoryView = button; 
+0

而這意味着你得到upvote! – Saltymule 2014-07-29 12:21:45

3

簡單記憶的變種JosephH。

如果你只是想裝飾「+」按鈕,繼續使用原來的didSelectRowAtIndexPath方法觸發,使用:

UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
[button setUserInteractionEnabled:NO]; 
cell.accessoryView = button; 

通過按鈕禁用用戶交互,它會傳播的基礎視圖壓力的事:細胞。