我有一个UICollectionView包含一些大的UICollectionViewCells.这些细胞非常大,以至于它们完全填满UICollectionView边界并延伸到屏幕外.
问题是大的单元格从中移除UICollectionView并排队等待重用,同时它们仍然显示,导致空白视图.如果我继续在空白UICollectionView区域滚动,最终会出现单元格的最后部分,并且下一个单元格的开始出现在正确的位置.
我已经有效地禁用了单元格重用,问题仍然明显发生,因为UICollectionView认为单元格不再显示,因为没有角落在集合视图的范围内.
为了演示创建一个单列的集合视图并且具有10000px高的单元格,当滚动非常高的单元格时,它将在大约1000px的内容滚动离开屏幕后消失,并将重新出现以显示最终的1000px内容对于细胞.
您可以下载一个显示此问题的简单原型应用程序:http: //jack.cox.s3.amazonaws.com/collectionviewprototype.zip
我在UIViewController中有一个UICollectionView.在collectionView cellForItemAtIndexPath:方法中,它基于数据源创建一系列自定义单元格.自定义单元格又包含一个UIView,子类用于绘制单个PDF页面.
它的设置方式是将PDF文件拆分为单页,因此单元格1包含PDF页面1,单元格2包含PDF页面2,依此类推.到目前为止很好,这是我的问题:
当我向下滚动时,UICollectionView开始显示错误的单元格.例如,在34页文档中,它以正确的顺序显示单元格/页面1-16,但随后开始显示似乎已经进一步出列的页面,例如单元格1,单元格2,单元格4.我从未到达任何地方靠近牢房/第34页.
我在过去看过UITableView的类似行为,并认为它与单元格的出列或委托方法有关.不太确定 - 任何帮助表示赞赏.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//create custom cell
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
//set file name (always the same; one PDF file)
cell.fileName = fileName;
cell.backgroundColor = [UIColor clearColor];
//set the title to the page number
cell.title = [NSString stringWithFormat:@"page %@", [countArray objectAtIndex:indexPath.row]];
//set the current page (which indicates which page to display) according to the pageCount
cell.currentPage = [[countArray objectAtIndex:indexPath.row] intValue];
return cell; }
Run Code Online (Sandbox Code Playgroud) 我的SuerClass UICollectionViewCell有一个属性:
var selected: Bool
Run Code Online (Sandbox Code Playgroud)
我的班级是
MyClass : UICollectionViewCell {
func setSelected(selected: Bool) {
super.selected = selected
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
前者在XCode 6.2中运行良好,但在XCode 6.3Beta2中它引发了一个错误:
Method 'setSelected' with Objective-C selector 'setSelected:' conflicts with setter for 'selected' from superclass 'UICollectionViewCell' with the same Objective-C selector
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题与XCode 6.3 beta2一起使用?
编辑:我也试过了
override func setSelected(selected: Bool) {
super.selected = selected
// do something
}
Run Code Online (Sandbox Code Playgroud)
这导致错误:
var selected: Bool
Run Code Online (Sandbox Code Playgroud) 最近,我遇到了Interface Builder无法解决的问题.我正在使用UICollectionViewController,每当我添加一个Cell时,它会立即显示一个橙色矩形(表示自动布局更新).更新帧/单元格不会改变任何内容.与橙色标记相关的警告显示"预期:高度= Y,实际:高度= X",其中Y 始终 精确为 X-100.也就是说:当我将自定义单元格高度设置为193时,它会显示"Expected:height = 93".无论我输入哪种尺寸,都会发生这种情况
这个问题是我对子视图及其约束有很多困难.例如,如果我添加一个带有约束的子视图以保持与单元格相同的高度,它将显示为93(预期高度),即使我的单元格应为193. Layouts也变得不可能.
我尝试添加一个新单元格,同样的问题.我尝试删除每个子视图并重新添加它们,没有.
任何想法都非常受欢迎.
非常感谢你!

目标:
为单元格高度的变化设置动画并重新定位周围的单元格.
场景:
集合视图中的某些单元格会加载远程图像.最初,这些单元格的大小是静态的,并显示了活动指示符.加载图像后,将其添加到其单元格中,并更改单元格的高度以适合照片.
笔记:
我用它来动画细胞的框架变化animateWithDuration.这样可以正常工作,除了增加的单元尺寸使其与下面的单元重叠.collectionView.collectionViewLayout invalidateLayout在调整目标单元格大小并更新返回的大小后,我盲目地尝试调用但sizeForItemAtIndexPath没有成功.
有什么建议?谢谢!
示例代码:

objective-c uicollectionview uicollectionviewcell uicollectionviewlayout ios7
我有一个相当复杂的collectionView单元格,我注意到如果我在我的collectionView上滚动得非常快,它会崩溃应用程序.
我得到的错误之一是:
negative or zero sizes are not supported in the flow layout
Run Code Online (Sandbox Code Playgroud)
我注意到如果我只是返回一个浮点值,例如500,在我的UICollectionView sizeForItemAtIndexPath:方法中,它不会崩溃.
我的集合视图具有动态单元格高度.
我正在sizeForItemAtIndexPath:使用此库在我的方法中解析HTML Attributed字符串:
https://github.com/mmislam101/HTMLAttributedString
任何人都知道是什么原因导致上述错误消息特别发生?
我在Bugsense报告中看到的与此崩溃有关的另一个错误是:
- [__ NSArrayM objectAtIndex:]:索引2超出边界[0 .. 1]
当我滚动得太快时会发生这种情况= /
Bugsense stacktrace显示崩溃顺序方法调用是:
1)collectionView:layout:sizeForItemAtIndexPath:
2)calculateFeedCellHeightForIndexPath :(这是我自己的方法之一,而不是Apple的方法)
3)dynamicHeightForHTMLAttributedString:UsingWidth:AndFont :(这是我自己的方法之一,而不是Apple的方法)
4)HTMLAttributedString attributionStringWithHtml:andBodyFont:第35行
5)HTMLAttributedString attributionString第79行
6)scrollViewDidScroll:第174行
7)setFeedFooterHeight:animated:第124行
我的setFeedFooterHeight:animated:方法是:
-(void)setFeedFooterHeight:(CGFloat)newHeight animated:(BOOL)animated
{
footerHeightConstraint.constant = newHeight;
if(animated)
{
[UIView animateWithDuration:0.35 animations:^{
[self layoutIfNeeded]; // <------ crash on this line it seems
}];
}
else
{
[self.feedFooterView layoutIfNeeded];
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我直接从Xcode运行应用程序而不是上面的Testflight时,Xcode在上面的步骤5停止,这产生了这段代码:
- (NSAttributedString …Run Code Online (Sandbox Code Playgroud) 我在故事板中有单元格的UICollectionView.每个单元格的大小设置为145x145.
它们在iPhone 4 - 5上显得很好,但尺寸并没有在iPhone 6和6+上按比例增加.不是为每个设备手动设置不同的单元大小,而是如何动态地设置它?
请参阅此处的示例:iPhone 5S:http://prntscr.com/55vy7q,iPhone 6:http://prntscr.com/55vyy2
我试图在UICollectionView中预先选择第一个对象/ UICollectionViewCell?我试过了:
self.dateCollectionView.allowsMultipleSelection=NO;
[self.dateCollectionView selectItemAtIndexPath:0 animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
[self collectionView:self.dateCollectionView didSelectItemAtIndexPath:0];
[self.dateCollectionView reloadData];
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad.
这是我的UICollectionView方法;
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.titles.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor= [UIColor clearColor];
UILabel * dateLabel = (UILabel *)[cell viewWithTag:1];
UILabel * subText = (UILabel *)[cell viewWithTag:2];
subText.text=self.titles[indexPath.row];
subText.adjustsFontSizeToFitWidth=YES;
if (cell.selected) {
cell.backgroundColor = [UIColor blueColor]; // highlight selection
}
else
{
cell.backgroundColor = [UIColor redColor]; // Default color
}
return cell;
}
-(void)collectionView:(UICollectionView …Run Code Online (Sandbox Code Playgroud) 我想将我的(BusinessViewTableHeader:UIView)作为tableView标头放置:
tableView.tableHeaderView = BusinessViewTableHeader.instanceFromNib() as! BusinessViewTableHeader
Run Code Online (Sandbox Code Playgroud)
在BusinessViewTableHeader内部有一个UICollectionView,它应该在刷卡时显示图像,就像Tinder应用程序一样.
这是我的UIView子类:
class BusinessViewTableHeader: UIView {
@IBOutlet var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.delegate = self
self.collectionView.registerNib(UINib(nibName: "BusinessImageCollectionCell", bundle: nil), forCellWithReuseIdentifier: "BusinessImageCollectionCell")
}
class func instanceFromNib() -> UIView {
return UINib(nibName: "BusinessViewTableHeader", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
}
....
}
extension BusinessViewTableHeader: UICollectionViewDelegate, UICollectionViewDataSource {
....
}
Run Code Online (Sandbox Code Playgroud)
我有一个包含UICollectionView的自定义UIView xib.问题是我无法将任何单元格(项)添加到UICollectionView.我可以将项添加到放在UIViewController中的其他UICollectionView.第一个图像显示UIViewController内UICollectionView的属性,第二个图像显示UIView xib内的UICollectionView.
[
2
为什么我无法将项添加到UIView xib中的UICollectionView?有办法做到这一点吗?
问题看起来像这样:http: //i.imgur.com/5iaAiGQ.mp4(红色是cell.contentView的颜色)
这是代码:https://github.com/nezhyborets/UICollectionViewContentsAnimationProblem
当前状态: UICollectionViewCell的contentView的内容不会与contentView框架更改一起动画.它立即获得大小没有动画.
执行任务时遇到的其他问题: contentView也没有动画到单元格的帧更改,直到我在UICollectionViewCell子类中执行此操作:
override func awakeFromNib() {
super.awakeFromNib()
//Because contentView won't animate along with cell
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
Run Code Online (Sandbox Code Playgroud)
其他说明: 以下是单元格大小动画中涉及的代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.selectedIndex = indexPath.row
collectionView.performBatchUpdates({
collectionView.reloadData()
}, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let isSelected = self.selectedIndex == indexPath.row
let someSize : CGFloat = 90 //doesn't matter
let sizeK …Run Code Online (Sandbox Code Playgroud) ios ×7
objective-c ×3
autolayout ×2
iphone ×2
swift ×2
animation ×1
inheritance ×1
ios7 ×1
overriding ×1
pdf ×1
uiview ×1