ios - 如何删除 UICollectionViewCell 中的间距?

标签 ios objective-c uicollectionview uicollectionviewcell

我正在使用第三方 UiCollection View KRLCollectionViewGridLayout。
在此,我在屏幕截图中附加了以下问题。

enter image description here

请参阅上面的屏幕截图。 CollectionView 单元格中有一个额外的空间。
我的问题是如何删除收集单元中的这些额外空间以及如何在两个单元之间添加分隔符?

这是我的代码:

- (KRLCollectionViewGridLayout *)layout
{
    return (id)self.collectionView.collectionViewLayout;
}

- (IBAction)changeColumnsTapped:(id)sender {
    [[[UIActionSheet alloc] initWithTitle:@"Choose how many columns"
                                 delegate:self
                        cancelButtonTitle:nil
                   destructiveButtonTitle:nil
                        otherButtonTitles:@"1",@"2", nil]
     showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.layout.numberOfItemsPerLine = [[actionSheet buttonTitleAtIndex:buttonIndex] integerValue];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, 44.0)];
    [self.collectionView addSubview:searchBar];
    [self.collectionView setContentInset:UIEdgeInsetsMake(44, 0, 0, 0)];

    self.layout.numberOfItemsPerLine = 1;
    self.layout.aspectRatio = 1;
    self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.layout.interitemSpacing = 10;
    self.layout.lineSpacing = 10;

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;

    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    //recipePhotos = [NSMutableArray arrayWithObjects:@"image1.png",@"image2.png",@"image3.png", nil];
     recipePhotos = [NSMutableArray arrayWithObjects:@"image1.png", nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [recipePhotos count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell1" forIndexPath:indexPath];

    //cell.contentView.backgroundColor = [UIColor blueColor];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];

    cell.layer.borderWidth = 0.5;

    return cell;
}

最佳答案

我是这个布局的作者。就目前而言,您似乎正在使用布局的 aspectRatio 属性的默认值 1/1,这是负责调整单元格大小的值。不幸的是,这个布局还不支持动态大小的单元格,但我可能会在未来支持。如果您知道您的单元格相对于它们的宽度有多高,您可以相应地设置纵横比。纵横比为 2 表示长度是高度的两倍,纵横比为 0.5 表示长度是高度的 1/2。您可能需要一些大于 1 的数字来减小间距。

如果这不能给你想要的效果,你可能需要切换到 UICollectionViewFlowLayout 而不是让你精确控制单元格的大小。

关于ios - 如何删除 UICollectionViewCell 中的间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30479803/

相关文章:

ios - "No valid iOS code signing keys found in keychain"

ios - 如何使用 Swift 的 Decodable 来解析您只知道或只关心几个字段的任意 JSON 字符串?

iOS 应用程序(xCode 替代品)

ios - 动画 CALayer 边框变化

ios - 单击选项卡时,选项卡栏 Controller 不显示 XIB UIViewController

ios - 单击 Collection View 中的单元格即可转换到 View Controller

ios - UICollection View 单元格的自定义单元格无法清除旧数据

ios - SwiftUI:如何 "swipe delete"嵌套 ForEach 中的元素(分组)

ios - UICollectionViewListCell 总是设置 44 的高度,这与我在 AutoLayout 中设置的任何其他内容冲突

ios - 只允许按一次表格单元格