我是新來的應用程序開發。在我的應用程序中,當我點擊我的表格視圖單元格時,單元格被選中,並且不管我是單擊圖像還是單元格,都會觸發該方法。我希望在點擊圖片時選擇圖片,而不是單元格。提前致謝。如何通過觸摸tableviewcell選擇圖像
回答
在圖像視圖中添加UITapGestureRecognizer,如果您不想單元格選擇,則關閉表格視圖單元格選擇。在圖像上點擊功能做你想要的。
在您的cellForRowAtIndexPath
方法中,添加一個自定義UIButton
並將該圖像指定爲該按鈕的背景圖像。然後添加目標按鈕和手柄的按鈕目標方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
UIButton *imageButton = [[UIButton alloc] initWithFrame:<Give the frame you want>];
[imageButton setBackgroundImage:<your image> forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(imageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:imageButton];
}
return cell;
}
在這裏,最好的方法是創建一個自定義UITableViewCell
具有imageButton
作爲一個實例變量,而不是內部cellForRowAtIndexPath
方法創建它的內部代碼。現在在imageButtonClicked
方法,以便它放在你想要的位置
-(void)imageButtonClicked:(UIButton *)sender {
UIImage *image = sender.currentBackgroundImage;
//Do the logic what you want here
}
請給按鈕的框架。
我還沒有試過這段代碼。如果sender.currentBackgroundImage
沒有給你圖像,那麼創建一個從UIButton擴展的自定義按鈕。自定義按鈕應該有一個UIImage變量作爲實例變量。將變量分配給初始化時所需的圖像(在cellForRowAtIndexPath
方法中)。然後你可以訪問按鈕目標方法中的圖像
我會很好,如果'UIButton * imageButton = [[UIButton alloc] .......... forControlEvents:UIControlEventTouchUpInside]; [cell addSubview:imageButton];'移動到自定義的UITableViewCell類。每次調用addSubview:代價都很高。 – byJeevan
是的,我同意你的意見。我這樣做是爲了向他展示它是如何完成的。最好的方法是創建一個自定義的UITableViewCell類 – Arun
如何用UITableViewCell Class做到這一點。建議我。 –
- 1. 圖像觸摸選擇區
- 2. 通過觸摸拖動圖像視圖
- 3. Unity3d通過觸摸選擇子對象
- 4. 通過觸摸iPhone繪製圖像sdk
- 5. 如何通過觸摸將圖像添加到視圖?
- 6. 如何通過觸摸選擇多個UILabels?
- 7. Android - 通過觸摸選擇遊戲板中的圖塊?
- 8. TableViewCell圖片更改時觸摸
- 9. 如何通過觸摸僅添加一個圖像
- 10. 如何通過在Android中觸摸來拖動圖像?
- 11. 如何通過觸摸事件在畫布上移動圖像?
- 12. 如何通過觸摸偵聽器來旋轉圖像?
- 13. 我如何顯示觸摸某個特定圖像時選擇的圖像?
- 14. 如何通過使用swift選擇圖像選擇器來鏡像圖像?
- 15. 如何通過360度不同的圖像旋轉圖像,觸摸事件?
- 16. 通過UIImageView觸摸?
- 17. 通過uiviews觸摸
- 18. 如何通過點觸sencha觸摸切換視圖2.0.0
- 19. 如何在圖像視圖上通過觸摸事件進行詳細視圖?
- 20. 如何在觸摸UIButton時通過UIView檢測觸摸?
- 21. 如何在動畫過程中觸摸圖像?或者我能否觸摸它?
- 22. 如何通過觸摸來旋轉SpriteNode?
- 23. 如何通過觸摸刪除CCnode?
- 24. 如何控制可通過觸摸
- 25. 如何通過UIAction而不觸摸UIButton
- 26. iPhone MapKit:通過觸摸MKMapView手動選擇位置(座標)
- 27. 如何調整,在android系統比其他圖像或視圖通過觸摸
- 28. Tizen選擇觸摸事件
- 29. iOS:UITableView選擇性觸摸
- 30. 如何插入DHTMLX觸摸圖像
你可以添加一個自定義按鈕到你的tableviewcell並給它的動作 – Arun
顯示你的嘗試代碼 –
請在SA中提問時更具體。向我們展示一些代碼你試過的東西 – Arun