我有一個tableview
,我該如何添加圖像到這個單元格的左邊?添加圖像到uitableview cell
回答
cell.imageView.image = [UIImage imageNamed:@"image.png"];
UPDATE: 像史蒂芬費舍爾表示,這應該只是爲了與風格UITableViewCellStyleDefault這是默認的風格細胞起作用。對於其他樣式,您需要將UIImageView添加到單元格的contentView。
試試這個代碼: -
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:@"arrow2.png"];
[cell addSubview:imv];
[imv release];
把上面的代碼裏面的方法 - (的UITableViewCell *)的tableView:(UITableView的*)的tableView \t \t的cellForRowAtIndexPath:(NSIndexPath *)indexPath { – 2011-05-03 12:56:58
代替直接加入到細胞使用的[cell.contentView addSubview:IMV]; – 2014-10-31 06:21:18
標準的UITableViewCell已經包含了UIImageView,如果它的圖像被設置,它會出現在所有標籤的左邊。您可以使用ImageView的屬性來訪問它:
cell.imageView.image = someImage;
如果由於某種原因,標準行爲不適合您的需要(請注意,您可以自定義標準圖像視圖屬性),那麼你可以將自己的UIImageView添加到小區阿曼在回答中建議。但是在這種方法中,您必須自己管理單元格的佈局(例如,確保單元格標籤不會與圖像重疊)。而且不添加子視圖直接的細胞 - 它們添加到單元格的內容查看:
// DO NOT!
[cell addSubview:imv];
// DO:
[cell.contentView addSubview:imv];
對於我的同胞SWIFT用戶,這裏是你需要的代碼:
let imageName = "un-child-rights.jpg"
let image = UIImage(named: imageName)
cell.imageView!.image = image
- 1. UITableView Cell上的圖像
- 2. 將UIImageView添加到自定義UITableView Cell
- 3. 將UIViews添加到cell時出現UITableView性能問題.contentView
- 4. UITableview cell subview larger height then cell
- 5. 如何添加圖像到UITableView單元格?
- 6. UITableView爲圖像圖標添加標題
- 7. 定製UITableView Cell
- 8. UITableView Center Cell Details
- 9. 添加UIButton到UItableview單元
- 10. 添加到UITableView
- 11. 圖像自動添加到UItableView單元格中?
- 12. 添加到UIButton的UITableView
- 13. 將圖標圖像添加到UITableview陣列
- 14. 如何將圖像添加到手機圖庫中的UITableView
- 15. 在UITableview的標題視圖中添加動態圖像到UIView
- 16. 添加選中標記圖像到選中的同一行UITableView
- 17. IOS(Objective-C)如何在UITableView Cell中無限期地更改UITableView Cell中的圖像
- 18. 如何從UITableView加載next/previous Cell?
- 19. 添加疊加圖像到CSS圖像?
- 20. 在每個datagrid中添加圖像gwt cell
- 21. 在TableView Cell中添加圖像不顯示
- 22. 從JSON中將縮略圖圖像加載到UITableView中
- 23. UITableView調整後臺線程自定義大小的圖像Cell
- 24. IOS優化加載圖像到UITableView
- 25. 如何將3個圖像添加到UITableView的每一行?
- 26. iPhone:將圖像添加到分組的uitableview部分標題?
- 27. 添加一個UITableView到UIViewController的視圖
- 28. 添加圖像到視圖
- 29. 返回到特定的UITableView Cell
- 30. uitableview重複圖像
如果你的單元格的UITableviewCellStyle屬性錯誤,它不會有'imageView',所以你將無法以這種方式設置圖像。我認爲只有'UITableViewCellStyleDefault'支持這種方式的圖像;否則,您需要添加自己的圖像視圖(請參閱Aman的答案)。 – 2012-02-07 23:34:53
其實如果你使用的是自定義單元格,你不應該像Aman那樣做 - 你應該添加視圖到cell.contentView,而不是直接添加到單元格。 – 2012-02-07 23:36:31
但關於單元格樣式,你是對的,只有默認單元格支持imageView - https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/c/econst/UITableViewCellStyleDefault – 2012-02-07 23:37:51