- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我正在以编程方式使用 UICollectionView。
我将它的框架设置如下:
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 3000) collectionViewLayout:flowLayout];
我的 UICollectionViewFlowLayout
有一个部分插图设置为:
flowLayout.sectionInset = UIEdgeInsetsMake(20.0, 20.0, 100.0, 20.0);
我也只有 1 个部分。
我已将高度设置为 3,000 以查看 Collection View 是否会滚动但它不会。我将其设置为该值,因为当我插入新项目时, Collection View 似乎不会向上或向下滚动。
UICollectionView
是 UIScrollView
的子类,这意味着我可以不断重置 ScrollView 内容大小。这能正常工作吗?
唯一的问题是,如果我知道所有项目的大小(包括每行有多少项目),这将起作用。
如果每个项目都有不同的大小,我如何找出每一行的大小?我需要它来添加所有行大小并增加 self.collectionView
的内容大小高度。
编辑
即使是我上面的建议,重置内容大小,也不能正常工作!更新我的 Collection View 时,我尝试了以下操作:
int numberOfRows = self.dataArray.count / 3;
self.collectionView.contentSize = CGSizeMake(self.view.frame.size.width, (numberOfRows * 200) + 300);
虽然这很丑陋。这是因为它假设我所有的图像都是 200x200px 大小,每行适合 3 张图像(因此除以 3)。
此外,即使我有 +300,最后一行我也只能看到大约 3/4 而不是全部。我必须滚动更多才能看到它,但它又回到了 3/4。它有点像要刷新的滚动条, View 只有在您拖动它时才会移动,而当您离开时它会撞回原来的位置。为什么会这样?难道只是Apple把UICollectionView
设计的太差了?这有点荒谬……UITableViews 会根据内容自动调整滚动条。
最佳答案
在回答您关于 UICollectionView 的一些其他问题时,我创建了这个演示项目,它滚动得很好,所以我不知道您遇到了什么问题。要使最后一行完全可见,我唯一需要做的就是将底部插图的大小增加到 90。我还在 performBatchUpdates:completion: 中添加了一个完成方法来自动滚动,以便最后添加的项目可见(请注意,我使用 performSelector;withObject:afterDelay:) 添加了一些额外的项目。我的图像是 48x48,我只是将我的 Collection View 设置为我的 Controller View 的边界。这是代码,您可以将其与现有代码进行比较:
@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> {
NSMutableArray *newData;
}
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *results;
@end
@implementation ViewController
- (void)viewDidLoad {
self.results = [NSMutableArray array];
int i = 11;
while (i<91) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"New_PICT00%d.jpg",i]];
[self.results addObject:image];
i++;
}
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
//[self.collectionView registerClass:[RDCell class] forCellWithReuseIdentifier:@"myCell"];
[self.collectionView reloadData];
[self performSelector:@selector(addNewImages:) withObject:nil afterDelay:3];
}
-(void)addNewImages:(id)sender {
newData = [NSMutableArray array];
int i = 102;
while (i<111) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"New_PICT0%d.jpg",i]];
[newData addObject:image];
i++;
}
[self addNewCells];
}
-(void)addNewCells {
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
[self.collectionView performBatchUpdates:^{
int resultsSize = [self.results count];
[self.results addObjectsFromArray:newData];
for (int i = resultsSize; i < resultsSize + newData.count; i++)
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
[self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
}
completion: ^(BOOL finished){
[self.collectionView scrollToItemAtIndexPath:arrayWithIndexPaths.lastObject atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
}];
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return [self.results count];
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
//cell.iv.image = [self.results objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
UIImage *image = [self.results objectAtIndex:indexPath.row];
return CGSizeMake(image.size.width, image.size.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 10, 90, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Selected Image is Item %d",indexPath.row);
}
关于objective-c - 设置部分插入时,带有 FlowLayout 的 UICollectionView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12679182/
public class MyFrame extends JFrame { public MyFrame(String title) { setSize(200, 200); setT
我有一个关于 QT 布局的小问题。 我有一个工具箱,我想用一些带有说明的可检查按钮来填充它。所以我创建了一个带有 QGridLayout 的 QWidget 并将 QButton 放在第一个单元格中,
我试图在 winform (C# .NET) 上获得一个非常简单的自动调整大小布局。我尝试过 TableLayoutPanels 和 FlowLayoutPanels 但没有任何效果。 我有一个用户控
我有一个关于 QT 布局的小问题。 我有一个工具箱,我想用一些带有说明的可检查按钮来填充它。所以我创建了一个带有 QGridLayout 的 QWidget 并将 QButton 放在第一个单元格中,
我正在尝试更改按钮大小,但是当我设置宽度时,即使高度有自己的值,高度也会自动更改。如何仅更改宽度(或高度)?我需要这个来我的“重置”和“计算”按钮。代码(没有不必要的其他代码) class Wind
是否有更简单的方法来删除 FlowLayout 中第一个组件前面的水平空间? 这基本上就是我的代码的样子: JPanel panel = new JPanel(new FlowLayout(FlowL
我有一个 flowlayout 面板,在调整大小事件中,我调整了 flowlayout 面板内所有控件的大小,使它们适合(flowlayoutpanel - 填充 - 滚动条宽度)的宽度。在某些调整大
所以,由于 FlowLayout(),我的 JFrame 没有按照我想要的方式运行,但我不知道还能用什么来修复这。它只会让我的 JButton 填满整个 JFrame。有没有办法让 FlowLayou
所以我有我的 Main,这是在里面完成的。 JFrame CF = new JFrame(); CF.setLayout(new BorderLayout()); CF.add(n
当我在具有流布局管理器的内容 Pane 中放置某些内容时,我会在该组件和内容 Pane 的边框之间获得空闲空间。 默认流布局的 hgap 和 wgap 非零,但将它们设置为零并不能解决问题。 注意:请
我在使用 UICollectionView 和相关布局时面临挑战。我正在尝试创建一个包含两列的垂直 UICollectionView,其中左右两列中的单元格的位置如图所示: 我确实设法毫无问题地创建了
我的 FlowLayout 确实将图片画得很小,所以我几乎看不到它们。 我的错误是什么? 我以后的计划是,它看起来像这样: 所以现在我正在研究内部流布局,它显示一行颜色分离或直方图。 publ
我有以下代码,可以在屏幕上生成两个单独的 .png。这些 .png 旨在通过鼠标移动,并且一切正常,除了当它们被拖动时,它们似乎位于某种类型的图层下方,并且当拖动超过一英寸时消失。非常感谢任何建议。
我正在使用 FlowLayout JPanel。当子组件高度不同时,面板看起来很难看。我正在寻找一种使它们顶部对齐的解决方案(类似于 HTML 中表格单元格的 valign="top")。 最佳答案
如标题中所述,我一直在尝试设置一种带有水平滚动的垂直流布局。布局中的组件将是 JLabel。让我画一张图: +-------------------------+ ========| ====
我正在使用 FlowLayout.CENTER 将字符串和一些复选框居中,但并没有将它们居中。我以前用过这个,效果很好。这是代码: import javax.swing.*; import java.
昨天我从这个网站的用户那里得到了很大的帮助,我决定在这里问另一个问题,希望你们能帮助我。我想编写一个包含组件的框架。它们需要位于 JPanel 上,并且 JPanel 应该位于 JScrollPane
我有两个面板: main_panel child_panel main_panel布局设置使用: main_panel.setLayout(flowlayout) 然后我添加了 child_panel
我想在 JTextField 和 JButtons 之间添加一些空间,我正在使用流布局,并且将不胜感激 文本字段和按钮将关闭,任何输入都会有帮助,我是新手 我正在制作一个计算器 代码 import j
我想将 FlowLayout 与 BorderLayout 内的 5 个标签一起作为北面板 (BorderLayout.NORTH),当我调整大小时窗口/框架我希望标签不消失而是移动到新行。 我一直在
我是一名优秀的程序员,十分优秀!