我有一个UICollectionView它工作正常,但我想以UICollectionViewCells编程方式将一些项添加到集合视图中.
那我该怎么做呢?
进一步澄清:当我以编程方式说我的意思是在运行时插入一个单元格,当一个动作被触发时,而不是在加载应用程序时(使用该viewDidLoad方法).我知道什么时候该模型被更新,调用是由UICollectionView在insertItemsAtIndexPaths:法.它应该创建一个新的单元格,但它没有这样做,它抛出一个错误.
如何调整集合视图的各个部分之间的间距.

iphone collectionview ios uicollectionview uicollectionreusableview
我正在做这个,我想使用CollectionView,但我还没有看过原型单元格,并且在这种情况下不知道如何使用CollectionView,有人可以帮助我吗?
我尝试使用这种方式,但它比UICollectionView需要大量的时间和管理
CollectionViewController.m第439行__50- [CollectionViewController photoLibraryDidChange:] _ block_invoke
致命异常:NSInternalInconsistencyException尝试删除并重新加载相同的索引路径({length = 2,path = 0 - 26007})
- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
// Call might come on any background queue. Re-dispatch to the main queue to handle it.
dispatch_async(dispatch_get_main_queue(), ^{
// check if there are changes to the assets (insertions, deletions, updates)
PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:self.assetsFetchResults];
if (collectionChanges) {
// get the new fetch result
self.assetsFetchResults = [collectionChanges fetchResultAfterChanges];
UICollectionView *collectionView = self.collectionView;
if (![collectionChanges hasIncrementalChanges] || [collectionChanges hasMoves]) {
// we need to reload …Run Code Online (Sandbox Code Playgroud) 我希望显示尽可能多的数组collectionViewCells,buttons因为我的数组中有字符串.但是当我启动模拟器时,只有背景CollectionView但没有显示细胞.可能是什么错误?
这是CollectionViewController我附加到CollectionViewmain.storyboard中的代码:
class CollectionViewController: UICollectionViewController {
var Array = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Array = ["1","2","3","4","5"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int) -> Int {
return Array.count
}
override func
collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var …Run Code Online (Sandbox Code Playgroud) 我有一个简单的问题,我无法在谷歌,文档或这里找到解决方案.
我的视图控制器中有一个Collectionview.我创建了一个包含UIImage的自定义单元格DescriptionCell.我希望这个图像有圆角.但是,我不知道在UIImage层上设置cornerradius的位置.我已尝试在单元格的awakeFromNib方法中,在委托方法CellForRowAtIndexPath中覆盖单元格中的LayoutSubview但它不起作用.我应该在哪里放置代码来设置UIImage的半径?
要指定,我知道如何创建UIImage的圆角.但如果它是Collectionview单元格的子视图,我不知道在哪里设置cornerradius.
这是我的descriptionCell的代码
class DescriptionCell: UICollectionViewCell {
@IBOutlet weak var mImage: UIImageView!
override func awakeFromNib() {
//mImage.layer.cornerradius = 5
//Does not work, the image is still square in the cell
}
Run Code Online (Sandbox Code Playgroud)
并在cellforRowAtIndePath中
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("descriptioncell", forIndexPath: indexPath) as! DescriptionCell
//cell.mImage.layer.cornerradius = 5
//Does not work, the image is still square in the cell
return cell
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
问题:
我有一个CollectionView,它将UIimage加载到每个单元格中.但是我的问题是,当我加载包含更多图像的其他单元格时,它们似乎会重复.我无法在我的代码中看到可能导致这种情况的原因.可能是因为可重复使用的细胞存在一些问题?
谁能明白为什么会这样?
注意:带有图像的阵列没有重复项
问题视频: https ://www.youtube.com/watch?v = vjRsFc8DDmI
问题形象:
这是我的collectionView函数:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
if self.movies == nil
{
return 0
}
return self.movies!.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell =
collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,
forIndexPath: indexPath) as! UpcomingCollectionViewCell
if self.movies != nil && self.movies!.count >= indexPath.row
{
// Calc size of cell
cell.frame.size.width = screenWidth …Run Code Online (Sandbox Code Playgroud) 到目前为止,我花了一半时间研究并试图了解如何制作一个包含多列的表格.令人尴尬的是,我对Swift和编程一般都很陌生,所以我读过很多东西并没有给我太多帮助.
我基本上找到了我想要用这位绅士的血腥创造的东西:http://www.brightec.co.uk/blog/uicollectionview-using-horizontal-and-vertical-scrolling-sticky-rows-and-columns
然而,即使和他的Github我仍然感到困惑.好像他根本没有使用Storyboard(对于我的项目,我一直在使用故事板).我假设这是正确的吗?
到目前为止我所拥有的是嵌入在导航控制器中的UICollectionView.从这里开始,我在CollectionView中创建了一个新的cocoa touch类文件.但是从这里开始我不知道该去哪里.
如果我可以有一些方向,从这里去哪里或如何正确设置,将非常感激.
非常感谢提前!
我是swift和iOS编程的新手,但我已经能够在Xcode中为我的应用程序构建一个基本稳定的启动界面.CollectionView从我家庭网络服务器上的csv文件创建的字典数组中获取图像和文本.cvs文件包含以下格式的数据(注意,已更改网址以保护许可图像):
csv文件
csv file is @ url https://myserver.com/csv.txt and contains the following
Title";"SeriesImageURL
Title1";"https://licensedimage.com/url1
Title2";"https://licensedimage.com/url2
...
Title1000";"https://licensedimage.com/url1000
Run Code Online (Sandbox Code Playgroud)
问题是当快速滚动CollectionView时,单元格将抓取不正确的图像.值得注意的是,如果您进行慢速或中速滚动,则在将正确的图像渲染到正确的单元格之前将显示不同的图像(单元格的标签文本始终是正确的,只有图像永远关闭).在图像与具有标签的正确单元格不匹配之后,CollectionView中的所有其他单元格也将显示不正确的图像.
例如,单元格1-9将显示Title1-9与正确的Image1-9当缓慢滚动时,单元格19-27将显示标题19-27,将简要显示图像10-18,然后显示正确的图像19-27.当快速滚动大量单元格(例如从单元格1-9到单元格90-99)时,单元格90-99将显示标题90-99,将显示图像10-50ish,然后将错误地保留在图像41-50上(或左右).当进一步滚动时,单元格100+将显示正确的标题,但仅显示图像41-50范围内的图像.
我认为这个错误或者是因为没有正确处理单元重用,没有正确处理图像的缓存,或者两者兼而有之.它也可能是我作为初学iOS/swift程序员看不到的东西.我试图使用完成修饰符实现一个请求,但似乎无法使我的代码设置方式正常工作.我将不胜感激任何帮助,并解释为什么修复工作的方式.谢谢!
相关代码如下.
SeriesCollectionViewController.swift
class SeriesCollectionViewController: UICollectionViewController, UISearchBarDelegate {
let reuseIdentifier:String = "SeriesCell"
// Set Data Source Models & Variables
struct seriesModel {
let title: AnyObject
let seriesimageurl: AnyObject
}
var seriesDict = [String:AnyObject]()
var seriesArray = [seriesModel]()
// Image Cache
var imageCache = NSCache()
override func viewDidLoad() {
super.viewDidLoad()
// Grab Data from Source
do {
let url = NSURL(string: "https://myserver.com/csv.txt") …Run Code Online (Sandbox Code Playgroud) collectionview ×10
ios ×10
swift ×6
objective-c ×3
custom-cell ×1
image ×1
ios8 ×1
ipad ×1
iphone ×1
photokit ×1
uiimage ×1
uiimageview ×1
uiview ×1
xcode ×1