UICollectionView
是要走的路。
您需要先載入所有本地頭像文件名。以下示例將加載以avatar-
開頭的app目錄中的所有圖像,忽略所有保留@2x.png
文件。
func getAvatarFilenames() -> Array<String> {
var avatarFileNames = Array<String>()
var paths = NSBundle.mainBundle().pathsForResourcesOfType("png", inDirectory: nil)
for path in paths {
var imageName = path.lastPathComponent
// ignore retina images as when the uiimage loads them back out
// it will pick the retina version if required
if (imageName.hasSuffix("@2x.png")) {
continue
}
// only add images that are prefixed with 'avatar-'
if (imageName.hasPrefix("avatar-")) {
avatarFileNames.append(imageName)
}
}
return avatarFileNames
}
然後,您可以創建一個加載每個頭像文件名的UICollectionView
。像這樣配置每個單元格(假設您的AvatarCell
具有標籤1000
的圖像 - 或更好,UICollectionViewCell
子類)。
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("AvatarCell", forIndexPath: indexPath) as UICollectionViewCell
var avatarImageView = cell.viewWithTag(1000) as UIImageView
avatarImageView.image = UIImage(named: avatarFileNames[indexPath.row])