我在IB中有一个带有一堆不同原型单元的UICollectionView.
我试图在IB中为每个单元格赋予自己的大小,这样当我使用dequeueReusable实例化它时,它将具有该大小.然而,我发现,无论你的IB中的设置对你的细胞是什么都没关系,你的集合视图使用它自己的细胞大小来处理它内部使用的所有细胞.
这是一个错误吗?如果不是,为所有单元格设置自定义大小的最佳方法是什么?
我也尝试过:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString* cellIdentifier = self.cellIdentifier;
ImageCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellIdentifier forIndexPath:indexPath];
return [cell sizeOfCell];
}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,应用程序在此功能中出现单元格时崩溃.
我已经将所有原型单元子类化,并且具有使它们返回自己大小的功能.是否还有其他方法可以使用您在IB中提供的标识符来实例化单元格?
我想UICollectionView在同一页面上有两个.两个UICollectionView将根据需要显示不同的数据.怎么做?先感谢您.
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifierHall2 = @"hall2";
/* Uncomment this block to use subclass-based cells */
timeCell *cell = (timeCell *)[myCollectionViewHall2 dequeueReusableCellWithReuseIdentifier:cellIdentifierHall2 forIndexPath:indexPath];
[cell.timeButton setTitle:[[allSimilarMutableArray valueForKey:@"showTime"] objectAtIndex:indexPath.item] forState:UIControlStateNormal];
return cell;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifierHall3 = @"hall3";
/* Uncomment this block to use subclass-based cells */
timeCell *cell = (timeCell *)[myCollectionViewHall3 dequeueReusableCellWithReuseIdentifier:cellIdentifierHall3 forIndexPath:indexPath];
[cell.timeButton setTitle:[[allSimilarMutableArray valueForKey:@"showTime"] objectAtIndex:indexPath.item] forState:UIControlStateNormal];
return cell;
}
Run Code Online (Sandbox Code Playgroud) 我有一个集合视图,并有自定义单元格,其中包含图像和标签.我已将集合视图设置如下 -
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
flowLayout.minimumLineSpacing = 150.0f;
flowLayout.minimumInteritemSpacing = 104.0f;
flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 100, 120);
_archiveCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
_archiveCollectionView.frame = CGRectMake(30, 218, _archiveCollectionView.frame.size.width - 60, _archiveCollectionView.frame.size.height - 350);
_archiveCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_archiveCollectionView.backgroundColor = [UIColor clearColor];
_archiveCollectionView.delegate = self;
_archiveCollectionView.dataSource = self;
[self.archiveCollectionView registerNib:[UINib nibWithNibName:@"FullArchiveEditionCell" bundle:nil] forCellWithReuseIdentifier:@"MyCell"];
[_archiveCollectionView reloadData];
[self.view addSubview:_archiveCollectionView];
Run Code Online (Sandbox Code Playgroud)
我还设置了以下方法:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _chosenCategoryArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath …Run Code Online (Sandbox Code Playgroud) 我已经实现了一个子类SPCell的UICollectionViewCell,我用我创造我的细胞UICollectionViewController.现在,当我收集一个牢房时,为了对它做点什么,我得到一个警告Incompatible pointer types initializing 'SPCell *' with an expression of type 'UICollectionViewCell *'
这是一个例子.我的自定义单元格保存图像,我想更改alpha值.但是在我分配单元格的行中,我得到了这个警告.
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;{
SPCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.thumbnail.alpha=0.6;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
在下面的代码中,collectionView将平滑滚动,但有时如果我滚动速度非常快,则会显示一个不正确的图像,然后在滚动减速时更改为正确的图像.为什么不设置下面是我正在使用的 代码我的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell;
cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
NSMutableArray *arr_assect=[menuArray objectAtIndex:indexPath.row];
backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)];
UIImageView *dotlines=[[UIImageView alloc]init];
dotlines.frame=CGRectMake(1, 0, cell.bounds.size.width-1, cell.bounds.size.height);
dotlines.image=[UIImage imageNamed:@"dottedline"];
[cell addSubview:dotlines];
UIImageView*cellImage=[[UIImageView alloc]initWithFrame:CGRectMake(18, 10,backView.bounds.size.height-35, backView.bounds.size.height-40)];
NSLog(@"arrayimage%@",[[menuArray valueForKey:@"category_image"] objectAtIndex:indexPath.row]);
NSString *str_homepage = [NSString stringWithFormat:@"%@",[[menuArray valueForKey:@"home_page"] objectAtIndex:indexPath.row]];
NSLog(@"str_home_page%@",str_homepage);
NSString *imageUrl= [arr_assect valueForKey:@"category_image"];
if ([[ImageCache sharedImageCache] DoesExist:imageUrl]) {
cellImage.image=[UIImage imageWithData:[[ImageCache sharedImageCache] GetImage:imageUrl]];
}
else{
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
dispatch_async(dispatch_get_main_queue(), ^{
cellImage.image=[UIImage …Run Code Online (Sandbox Code Playgroud) objective-c ios uicollectionview uicollectionviewcell uicollectionviewdelegate
我正在尝试使用静态分组单元格创建表格视图.见下图:
我还创建了一个"SettingsTableViewController.swift"类并将其附加到此视图.
但是当我运行应用程序时,表视图变为空白.如果我取消连接"SettingsTableViewController.swift"文件,则会显示静态分组的单元格.
在app build上,屏幕如下:
还有一点需要指出的是,我使用以下代码手动从一个segue转换到另一个segue:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("accountSettings") as! SettingsTableViewController
var fullname: AnyObject = self.username
var job: AnyObject = self.jobTitle
vc.username = fullname
vc.jobTitle = job
self.presentViewController(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
SettingsTableViewController代码:
import UIKit
class SettingsTableViewController: UITableViewController {
var username:AnyObject = ""
var jobTitle:AnyObject = ""
@IBOutlet weak var name: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
name.text? = username as! String
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning() …Run Code Online (Sandbox Code Playgroud) [![collectionview example] [1]] [1]
我试图使用集合视图模拟此行为.我已经开始使用这种方法,它让我更接近.虽然当我在水平分页CollectionView上进一步向右滑动时,内容会进一步向左移动.电池之间的间距也是关闭的.我认为它需要自定义布局,但我不确定.
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width - 20, height: collectionView.frame.height - 20)
}
Run Code Online (Sandbox Code Playgroud) 我试图以UICollectionViewCell编程方式创建一个子类.我正在尝试使用自动布局支持创建.
我想要UIView添加前导,顶部,尾部和底部约束的单元格的内容视图.但是当我这样做时,我的UIView's宽度和高度保持为零.这是我的代码内部initWithFrame方法:
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
_someView = [[UIView alloc] initWithFrame:CGRectZero];
_someView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_someView];
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-2-[someView]-2-|" options:0 metrics:nil views:@{@"someView" : _someView}];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-2-[someView]-2-|" options:0 metrics:nil views:@{@"someView" : _someView}];
[self.contentView addConstraints:horizontalConstraints];
[self.contentView addConstraints:verticalConstraints];
Run Code Online (Sandbox Code Playgroud)
为什么这些限制是灰色的?
谢谢!
我正在尝试从我的firebase数据库下载图像并将它们加载到collectionviewcells中.图像下载,但我无法让它们全部下载和异步加载.
目前,当我运行我的代码时,下载的最后一个图像加载 但是,如果我更新我的数据库,集合视图会更新,并且新的最后一个用户配置文件图像也会加载,但其余部分都会丢失.
我宁愿不使用第三方库,所以任何资源或建议将不胜感激.
这是处理下载的代码:
func loadImageUsingCacheWithUrlString(_ urlString: String) {
self.image = nil
// checks cache
if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
self.image = cachedImage
return
}
//download
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
//error handling
if let error = error {
print(error)
return
}
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
})
}).resume()
} …Run Code Online (Sandbox Code Playgroud) 我试图在下载其图像时在每个集合视图单元格中放置一个UIActivityIndicatorView.我把它出现在每个细胞中,但它拒绝自我中心.它停留在左上角.我怎样才能让它正确居中?
我是这样做的:
extension UIView {
func showActivityIndicator(onView: UIView, withIndicator: UIActivityIndicatorView) {
withIndicator.frame = CGRect(x: onView.frame.midX - 20, y: onView.frame.midY - 20, width: 40, height: 40)
withIndicator.activityIndicatorViewStyle = .whiteLarge
withIndicator.center = onView.center
onView.addSubview(withIndicator)
withIndicator.startAnimating()
}
}
Run Code Online (Sandbox Code Playgroud)
我在cellForItemAtIndexPath中调用该函数,如:
showActivityIndicator(onView: cell.contentView, withIndicator: activityInd)
Run Code Online (Sandbox Code Playgroud)
但我所做的一切都不会从左上角移动它.有什么建议?