我有一个UICollectionView.该UICollectionView的datasource是NSArray学生(从CoreData).我希望当前的学生项目是selected/ highlighted.
我怎样才能做到这一点?我知道有一种方法:
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath
animated:(BOOL)animated
scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
Run Code Online (Sandbox Code Playgroud)
作为参数a NSIndexPath和UICollectionViewScrollPosition.
我有数据源(NSArray由学生填充CoreData)和学生对象selected.
那么,我怎么得到NSIndexPath和UICollectionViewScrollPosition?或者有没有其他方法来突出显示项目?
我想连续显示两个单元格,无论iPhone的屏幕大小是多少.喜欢,

我的故事板包含一个UICollectionView由约束连接的故事板.
现在,当我在6,5s或更低版本中运行时,只有一个单元格出现在一行中.我用的代码是,
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [categoryImages count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
homePageCells *cell = (homePageCells *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cells" forIndexPath:indexPath];
cell.categoryName.text = [categoryNames objectAtIndex:indexPath.row];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用代码检测屏幕大小并以编程方式分配适当的单元格大小,
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize sizes;
CGSize result = [[UIScreen mainScreen] bounds].size;
NSLog(@"%f",result.height);
if(result.height == 480)
{
//Load 3.5 inch xib
sizes =CGSizeMake(170.f, 170.f);
NSLog(@"3gs");
}
else if(result.height == 568)
{
//Load 4 inch xib
sizes =CGSizeMake(130.f, 130.f);
NSLog(@"5s"); …Run Code Online (Sandbox Code Playgroud) 我想动画UICollectionViewCell调用何时动作.
我已经做UICollectionViewCell的Interface Builder,该UICollectionView还.现在我想indexPath在我的actionBtnAddToCard方法中得到正确的答案.
这就是我现在尝试的方式(ProduktViewCell.m中的方法):
- (IBAction)actionAddToCart:(id)sender {
XLog(@"");
// see this line
NSIndexPath *indexPath = ??** how can i access the correct indexPath**??;
SortimentViewController *svc = [[SortimentViewController alloc] initWithNibName:@"SortimentViewController_iPad" bundle:[NSBundle mainBundle]];
[svc.collectionViewProdukte cellForItemAtIndexPath:indexPath];
[svc collectionView:svc.collectionViewProdukte didSelectItemAtIndexPath:indexPath];
}
Run Code Online (Sandbox Code Playgroud)
SortimentViewController是viewController,它继承了UICollectionView.
如何访问正确的indexPath?
更新1:编辑帖子以便更好地理解.
nsindexpath ibaction ios uicollectionview uicollectionviewcell
我想显示一个UICollectionView,每行包含2行和100个单元格.
// 1
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return 100;
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 2;
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.lbl.text = @"Data";
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我得到这个:

如何为UICollectionView指定行号?我想创建一个2x100表.
我有一个UICollection,显示锁定到未登录用户的单元格上的挂锁.用户可以查看集合,然后以模态登录.当模态解散时,我试图重新加载表格的单元格和嵌套集合以从单元格中移除挂锁.
移除挂锁后,可见细胞不清爽.滚动集合时,屏幕外的单元格正确并显示挂锁.我在tableview和每个嵌套的collectionview上调用reloaddata().
我的代码分为:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SectionWorkouts", forIndexPath: indexPath) as! SectionTableViewCell
cell.delegate = self
// Return the workouts count from the section
let intIndex = indexPath.row
let index = workoutSections.startIndex.advancedBy(intIndex)
let currentWorkoutSectionKey = workoutSections.keys[index]
if let currentWorkoutSection = workoutSections[currentWorkoutSectionKey] {
cell.workoutsCollection.dataSource = sectionWorkoutsCell
cell.workoutsCollection.delegate = sectionWorkoutsCell
cell.updateCellWithWorkouts(currentWorkoutSectionKey, workouts: currentWorkoutSection)
}
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
class SectionTableViewCell: UITableViewCell,UICollectionViewDelegate, UICollectionViewDataSource {
var workouts = [Workout]()
var delegate: WorkoutCellDelegate?
@IBOutlet weak …Run Code Online (Sandbox Code Playgroud) 我在我的ios应用程序中使用CollectionView.每个集合单元格都包含一个删除按钮 通过单击按钮,应删除单元格.删除后,该空间将填充下面的单元格(我不希望重新加载CollectionView并再次从顶部开始)
如何使用autolayout从UICollectionView中删除特定单元格?
ios uicollectionview uicollectionviewcell uicollectionviewlayout
我在重用单元格和UICollectionViewiOS 7上遇到了问题.我的代码在iOS 6上工作正常,但在iOS 7中,它在调用后不重用单元格dequeueReusableCellWithReuseIdentifier(不要调用prepareForReuse).
即使这个代码https://developer.apple.com/library/ios/samplecode/CollectionView-Simple/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012860也不会重复使用iOS7上的单元格(但在iOS6上运行良好) ),每dequeueReusableCellWithReuseIdentifier创建一个新单元格并释放旧单元格.是否有一些新的东西阻止细胞重复使用?
我的细胞足够大,不再重复使用它们效率很低.我注意到iOS 7上的延迟,但iOS 6上没有,因为它们没有在iOS 7中重复使用.
cocoa-touch reusability uicollectionview uicollectionviewcell ios7
随着iOS 10的推出,似乎我们将在UITableView和UICollectionViews上默认启用预取.这意味着在用户实际看到它们之前,将获取未在屏幕上显示的单元格.
以下是一些相关方法:
nil如果单元格不可见".nil如果单元格不可见".所有这些都在他们的描述中特别提到"可见".随着在iOS 10中引入预取,我如何区分预取的单元与当前可见的单元?
换一种说法:
看起来UITableView或UICollectionView上没有任何新的API可以帮助解决这个问题.
嗨,我正试图通过改变细胞大小auto layout.
我想用3乘3显示单元格.
第一个Cell的边距= 0
最后一个单元格的边距= 0
并且所有小区的空间都是1pt.像一个instagram.
我应该设置为Cell的尺寸吗?我希望通过Autolayout设置约束.
我也尝试使用代码设置单元格的大小.
这是我的代码:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(123, 123);
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1;
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1;
}
Run Code Online (Sandbox Code Playgroud)
但是......我认为它并不完全可以计算出细胞的大小.
如何根据屏幕尺寸设置单元格的大小?
我必须在单元格之间设置1pt的空间.
无论如何只能使用故事板设置?
如果没有,我如何设置代码?
谢谢.
我希望我的UICollectionViewCells有圆角和阴影,但我遇到了一个问题,似乎我只能有一个或另一个,但不是两个.
为了绕过角落我在单元格的初始化中使用此代码:
CALayer *layer = [self layer];
[layer setCornerRadius:4];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
Run Code Online (Sandbox Code Playgroud)
要添加一个投影,我在单元格的初始化中使用此代码:
CALayer *layer = [self layer];
[layer setMasksToBounds:NO];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
[layer setShadowColor:[[UIColor blackColor] CGColor]];
[layer setShadowOffset:CGSizeMake(0.0f,0.5f)];
[layer setShadowRadius:8.0f];
[layer setShadowOpacity:0.2f];
[layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]];
Run Code Online (Sandbox Code Playgroud)
要尝试使用圆角和投影,我在单元格的初始化中使用此代码:
CALayer *layer = [self layer];
[layer setMasksToBounds:NO];
[layer setCornerRadius:4];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
[layer setShadowColor:[[UIColor blackColor] CGColor]];
[layer setShadowOffset:CGSizeMake(0.0f,0.5f)];
[layer setShadowRadius:8.0f];
[layer setShadowOpacity:0.2f];
[layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]];
Run Code Online (Sandbox Code Playgroud)
但这只会产生阴影.
这是一个错误还是我做错了什么?
ios ×7
swift ×2
uitableview ×2
calayer ×1
cocoa-touch ×1
ibaction ×1
ios10 ×1
ios7 ×1
iphone ×1
nsindexpath ×1
objective-c ×1
reusability ×1
storyboard ×1