2014-11-21 75 views
4

我已經創建了一個UIcollectionView和一個包含一些字符串@ [@「Item One」,「Item Two」,@「Item Three」]的數組;在uicollectionviewcell中設置文本

在泰伯維我這樣做:

NSString *object = self.titlesArray[indexPath.row]; 
cell.textLabel.text = object; 

但我真的不知道如何在UIcollectionView項目做到這一點。

+0

一個好地方開始:http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12。谷歌也很有幫助 – fguchelaar 2014-11-21 10:51:48

回答

9

UICollectionViewCell沒有默認的單元格樣式。 你必須創建一個自定義UICollectionViewCell並在裏面添加一個UILabel

3

UICollectionViewCell沒有默認textLabel作爲UITableviewCell有,您必須根據您的需要創建自定義UICollectionViewCell

你可以看看this教程如何創建自定義集合視圖單元格。

0

像已發佈的其他人一樣,有UILabel類型的財產。如果您不瞭解課程,請按CMD,然後選擇您所從事的課程。在UICollectionView,你會看到的情況下,有UICollectionViewCellFoundation內沒有定義屬性(:

NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewCell : UICollectionReusableView 

@property (nonatomic, readonly) UIView *contentView; // add custom subviews to the cell's contentView 

// Cells become highlighted when the user touches them. 
// The selected state is toggled when the user lifts up from a highlighted cell. 
// Override these methods to provide custom UI for a selected or highlighted state. 
// The collection view may call the setters inside an animation block. 
@property (nonatomic, getter=isSelected) BOOL selected; 
@property (nonatomic, getter=isHighlighted) BOOL highlighted; 

// The background view is a subview behind all other views. 
// If selectedBackgroundView is different than backgroundView, it will be placed above the background view and animated in on selection. 
@property (nonatomic, retain) UIView *backgroundView; 
@property (nonatomic, retain) UIView *selectedBackgroundView; 

@end 
0

我有同樣的問題,所以我做了一個小型的教程要做到這一點:How to make a simple collection view with Swift的代碼是斯威夫特但是這個過程是有關目的C.同一

的主要步驟是

  • 添加UICollectionView到視圖控制器在故事板
  • 將UILabel添加到集合視圖單元格。
  • 製作UICollectionViewCell的自定義子類以保存單元格標籤出口。
  • 使集合視圖單元格使用該類。
  • 在視圖控制器中實現UICollectionViewDataSourceUICollectionViewDelegate及其方法。
  • 連接所有的網點。
  • 使用數組或其他數據源中的字符串來填充單元格。