我试图突出显示UICollectionView中具有黄色边框的选定集合单元格,以便用户可以看到当前选择了哪一个.我试过这个:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
FilterCell *filterCell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"FilterCell" forIndexPath:indexPath];
filterCell.window.backgroundColor = [UIColor yellowColor];
filterCell.backgroundColor = [UIColor yellowColor];
NSLog(@"hello");
}
Run Code Online (Sandbox Code Playgroud)
在UICollectionViewCell内的UIImageView周围有2个空像素,所以它应该可以工作,但事实并非如此.
它正在记录"你好",但边框保持黑色.看这个截图:

有UIViewController哪些包含UICollectionView.还有UICollectionViewCell自己的类和XIB(CurrentCell.h,CurrentCell.m,CurrentCell.xib).
在我的UIViewController.h:
@property (strong, nonatomic) IBOutlet UICollectionView *collection;
Run Code Online (Sandbox Code Playgroud)
在我的UIViewController.m:
@synthesize collection;
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad中:
[self.collection registerClass:[CurrentCell class] forCellWithReuseIdentifier:@"cellCurRecipe"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(120, 90)];
[flowLayout setSectionInset:UIEdgeInsetsMake(5, 10, 5, 10)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.collection setCollectionViewLayout:flowLayout];
Run Code Online (Sandbox Code Playgroud)
在cellForItemAtIndexPath中:
static NSString *CellIdentifier = @"cellCurRecipe";
CurrentCell *cell = (CurrentCell *)[self.collection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
Recipes *recipe = [currRecipesArray objectAtIndex:indexPath.item];
[cell.image setImage: recipe.image];
return cell;
Run Code Online (Sandbox Code Playgroud)
但是:如果我直观地点击了单元格didSelectItemAtIndexPath方法调用.
EDITED
现在一切正常!
我忘了在CurrentCell.m中初始化单元格:
- (id)initWithFrame:(CGRect)frame …Run Code Online (Sandbox Code Playgroud) 我试图减少我的UICollectionViewCells之间的空间
我尝试了子类化UICollectionViewFlowLayout并重写此方法:
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for(int i = 1; i < [answer count]; ++i) {
UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
NSInteger maximumSpacing = 4;
NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return answer;
}
Run Code Online (Sandbox Code Playgroud)
然后在我的UICollectionViewController viewDidLoad中:
SublassedcollectionViewLayout *space = [[SublassedcollectionViewLayout alloc]init];
self.collectionView.collectionViewLayout = space;
Run Code Online (Sandbox Code Playgroud)
这只会让我的UICollectionViewCells更小. …
我有UIImageView就像简单的Gallery,可以从JSON获取数据.
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
NSArray *tmp =[allDataDictionary objectForKey:@"gallery"];
if (tmp.count>0){
for (NSDictionary *diction in tmp) {
[self.patternImagesArray addObject:diction];
}
NSLog(@"%@", self.patternImagesArray);
// [loading stopAnimating];
}
[self.collectionView reloadData];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PatternView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PatternCell" forIndexPath:indexPath];
NSString *myPatternString = [[self.patternImagesArray objectAtIndex:indexPath.row] valueForKey:@"thumb"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:myPatternString]];
cell.patternImageView.image = [UIImage imageWithData:data];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
为了给出完整的图像我使用这个:
-(void)viewDidLoad{
NSString *path=[_newsData valueForKey:@"bigimg"];
NSData* imageData = [NSData dataWithContentsOfURL: [NSURL URLWithString: path]];
UIImage* image …Run Code Online (Sandbox Code Playgroud) 我正在尝试添加UIButton一个UICollectionViewCell.该按钮的主要目的是让用户选择他们的收藏夹.
当他们点击时UIButton,UIButton会显示一个发光的星形图像,显示该项目已被选为收藏.
UIButton再次单击,按钮返回以显示带有空星的原始图像,这意味着取消选择.
我意识到这一点的方式如下:
1 - 在故事板中,将UIButton放入UICollectionViewCell,按住Ctrl键将按钮拖动到单元格的.h文件中以设置一个名为
-(IBAction)favoriteBtnClick:(id)sender;
Run Code Online (Sandbox Code Playgroud)
2 - 在单元格的.m文件中,按如下方式编辑操作:
-(IBAction)favoriteBtnClick:(id)sender {
if ([self.favoriteChecked isEqualToString:@"NO"]) {
UIImage *image = [UIImage imageNamed:@"FavoriteSelected.PNG"];
[sender setBackgroundImage:image forState:UIControlStateNormal];
self.favoriteChecked = @"YES";
} else if ([self.favoriteChecked isEqualToString:@"YES"]) {
UIImage *image = [UIImage imageNamed:@"FavoriteBlank.PNG"];
[sender setBackgroundImage:image forState:UIControlStateNormal];
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行时UICollectionView,当我单击一个单元格上的按钮时,发生了"奇怪"的行为:
1 - 在向下滚动列表后,单击一个单元格将一个按钮转到最喜欢的发光星形,下面其他一些单元格上的按钮也会被选中.
2 - 切换仅在一个周期内起作用.完成第一轮选择和取消选择后,进一步点击后不再有反应.
任何人都可以帮我理解这个的原因,并可能给我一些如何解决它的提示?
谢谢!
问候,保罗
我UICollectionView在水平流布局中显示自定义单元格; 换句话说,一些内容被放置在屏幕边界之外.另外,我有一个触发NSNotification导致我的细胞的一些元素(即主题)的颜色变化的手势.除了屏幕边界外的单元格不会更新到新的颜色变化这一事实外,一切都很完美.有没有办法强迫他们重绘?
在时调用的函数NSNotification被激发我试着与重绘收集观点self.collectionView.reloadData(),self.collectionView.setNeedsDisplay()与self.collectionView.setNeedsLayout但无济于事.我尝试了awakeFromNib()自定义单元类中列表的最后两个但没有.
这是我的代码cellForItemAtIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = popularCollectionView!.dequeueReusableCellWithReuseIdentifier("popular", forIndexPath: indexPath) as! PopularPainting
cell.thumbnail.image = paintings[indexPath.row].paintingImage
cell.name!.text = paintings[indexPath.row].paintingName
cell.price!.setTitle(paintings[indexPath.row].paintingPrice, forState: UIControlState.Normal)
if cell.isDark {
cell.name!.textColor = UIColor(red: 205/255, green: 205/255, blue: 205/255, alpha: 1)
cell.price!.setTitleColor(self.kPaleBlue, forState: .Normal)
self.popularCollectionView.reloadData()
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
注意:滚动到屏幕外的内容并重复手势以更改主题非常有效,所以我不知道是什么.
这是来自UICollectionViewCell动态调整4.4和5.5英寸的后续问题
虽然这个解决方案很棒,但我想根据设备的宽度,宽高比和我必须显示的总项目数量在UIVIewController中生成动态数量的项目单元格(kCellsPerRow).我只是无法弄清楚如何计算正确数量的kCellsPerRow,以便一旦达到宽高比限制它们就不会过于调整大小.
这是我到目前为止所拥有的:
var kCellsPerRow = ceil(global.TotalAmount) //Total Amount of Cells
var windowWidth = layout.collectionView!.frame.width // Device's Window Width
var dynamicWidth = windowWidth / CGFloat(kCellsPerRow) // Current Width of Each Cell
var dynamicHeight = windowWidth / CGFloat(kCellsPerRow) // Current Height of Each Cell
var limit = (Double(dynamicWidth) / Double(windowWidth))*(9/2) < (2/9) // Constraint/Threshold
if ( limit ) {
var newkCellsPerRow = CGFloat(kCellsPerRow * (2/9)) // Attempt
newkCellsPerRow = 9 // Fixed Size of 9 but what if we …Run Code Online (Sandbox Code Playgroud) dynamic uicollectionview uicollectionviewcell uicollectionviewlayout swift
这是参考链接:
https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest
我已UICollectionView在最后一个单元中实现了更多加载,数据正在下载,下载完成后我想重新加载集合
collectionView.collectionViewLayout.invalidateLayout()
self.collectionView.reloadData()
let concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(concurrentQueue) {
DataManager.sharedInstance.homeRequest(Dictionary: params, onSuccess: { (dict) in
self.downloadPageIndex += 1
let response = HomeObject(dictionary: dict)
for (_,e) in (response.dataDict?.enumerate())!{
self.dataArray.append(e)
}
dispatch_async(dispatch_get_main_queue()) {
self.collectionView.reloadData()
self.collectionView.collectionViewLayout.invalidateLayout()
}
self.activityIndicatorCell.stopAnimating()
}, onError: { (error) in
self.activityIndicatorCell.stopAnimating()
})
Run Code Online (Sandbox Code Playgroud)
像这个collectionview reloadata
谁能帮我吗?
我能够使用下面的函数更改collectionView单元格大小但是如何使此功能适用于不同的屏幕大小.所以,例如我希望iPad上的高度和宽度不同,iPhone就有办法做到这一点
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
return CGSize(width: (width/5.5), height: (height/4.5))
}
Run Code Online (Sandbox Code Playgroud) ios uicollectionview uicollectionviewcell uicollectionviewlayout swift
我希望在我的UICollectionView中重复使用3个小单元,然后是2个大单元,再重复3个小单元,
我知道它与这个功能有关,但我很困惑如何实现结果
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
}
Run Code Online (Sandbox Code Playgroud)
请看一下截图.
ios uicollectionview uicollectionviewcell uicollectionviewlayout swift
ios ×8
swift ×5
objective-c ×2
dynamic ×1
json ×1
spacing ×1
uibutton ×1
uiimageview ×1
xcode ×1