- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含 UICollectionView
的 UITableViewCell
。每个 Collection View 最多可以有 3 个单元格。我正在使用此代码添加单元格。
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
collectionIndex++;
return [[collectionCellData objectAtIndex:collectionIndex] count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"collection Cell: %@", indexPath);
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"availableTimeCell" forIndexPath:indexPath];
NSLog(@"Data: %@", [collectionCellData objectAtIndex:collectionIndex]);
UILabel *label = [[UILabel alloc] initWithFrame:cell.frame];
[label setFont:[UIFont fontWithName:@"Avenir Next" size:13.0]];
[label setTextColor:[UIColor colorWithRed:0.108 green:0.550 blue:1.000 alpha:1.000]];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *startTimeString = [[[collectionCellData objectAtIndex:collectionIndex] objectAtIndex:indexPath.row] valueForKey:@"startTimestamp"];
NSLog(@"Start Time String: %@", startTimeString);
NSDate *date = [dateFormatter dateFromString:startTimeString];
NSLog(@"New Date: %@", date);
[dateFormatter setDateFormat:@"h:mm a"];
NSString *newDate = [dateFormatter stringFromDate:date];
NSLog(@"NEW NEW Date: %@", newDate);
label.text = newDate;
[cell addSubview:label];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(53, 17);
}
我的 cellForRowAtIndexPath
是
- (UITableViewCell *)tableCeption:(BJTableCeption *)tableCeption cellForItem:(id)item
{
NSArray *topLevelObjects;
EngagementListCell *cell;
if ([item isKindOfClass:[BJItemObject class]]) {
cell = (EngagementListCell *)[tableCeption dequeueReusableCellWithIdentifier:@"EngagementListCell"];
if (cell == nil) {
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EngagementListCells" owner:nil options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
cell.lblStaff.text = [((AppointmentGrid *)((BJItemObject *)item).item).employeeObject valueForKey:@"displayName"]; // Staff name
cell.availableTimeCollection.dataSource = self;
cell.availableTimeCollection.delegate = self;
[cell.availableTimeCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"availableTimeCell"];
[collectionCellData addObject:[NSMutableArray arrayWithArray:[((AppointmentGrid *)((BJItemObject *)item).item) slots]]];
NSLog(@"cell data: %@", collectionCellData);
[cell.availableTimeCollection reloadData];
} else {
cell = (EngagementListCell *)[tableCeption dequeueReusableCellWithIdentifier:@"ServiceHeaderCell"];
if (cell == nil) {
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EngagementListCells" owner:nil options:nil];
cell = [topLevelObjects objectAtIndex:1];
}
cell.lblService.text = ((BJCategoryObject *)item).item; // Service name
}
return cell;
}
日志输出
2014-01-28 08:39:42.818 Customer View[30896:70b] Start Time String: 2014-01-30 08:00:00
2014-01-28 08:39:42.819 Customer View[30896:70b] New Date: 2014-01-30 15:00:00 +0000
2014-01-28 08:39:42.819 Customer View[30896:70b] NEW NEW Date: 8:00 AM
2014-01-28 08:39:42.819 Customer View[30896:70b] collection Cell: <NSIndexPath: 0x9099470> {length = 2, path = 0 - 0}
2014-01-28 08:39:42.820 Customer View[30896:70b] Data: (
{
endTimestamp = "2014-01-30 08:05:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:00:00";
}
)
2014-01-28 08:39:42.820 Customer View[30896:70b] Start Time String: 2014-01-30 08:00:00
2014-01-28 08:39:42.821 Customer View[30896:70b] New Date: 2014-01-30 15:00:00 +0000
2014-01-28 08:39:42.821 Customer View[30896:70b] NEW NEW Date: 8:00 AM
2014-01-28 08:39:42.821 Customer View[30896:70b] collection Cell: <NSIndexPath: 0x90a3130> {length = 2, path = 0 - 0}
2014-01-28 08:39:42.822 Customer View[30896:70b] Data: (
{
endTimestamp = "2014-01-30 08:05:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:00:00";
},
{
endTimestamp = "2014-01-30 08:10:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:05:00";
},
{
endTimestamp = "2014-01-30 08:15:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:10:00";
}
)
2014-01-28 08:39:42.822 Customer View[30896:70b] Start Time String: 2014-01-30 08:00:00
2014-01-28 08:39:42.822 Customer View[30896:70b] New Date: 2014-01-30 15:00:00 +0000
2014-01-28 08:39:42.823 Customer View[30896:70b] NEW NEW Date: 8:00 AM
2014-01-28 08:39:42.823 Customer View[30896:70b] collection Cell: <NSIndexPath: 0x90d7eb0> {length = 2, path = 0 - 1}
2014-01-28 08:39:42.823 Customer View[30896:70b] Data: (
{
endTimestamp = "2014-01-30 08:05:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:00:00";
},
{
endTimestamp = "2014-01-30 08:10:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:05:00";
},
{
endTimestamp = "2014-01-30 08:15:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:10:00";
}
)
2014-01-28 08:39:42.823 Customer View[30896:70b] Start Time String: 2014-01-30 08:05:00
2014-01-28 08:39:42.824 Customer View[30896:70b] New Date: 2014-01-30 15:05:00 +0000
2014-01-28 08:39:42.824 Customer View[30896:70b] NEW NEW Date: 8:05 AM
2014-01-28 08:39:42.824 Customer View[30896:70b] collection Cell: <NSIndexPath: 0x9091980> {length = 2, path = 0 - 2}
2014-01-28 08:39:42.825 Customer View[30896:70b] Data: (
{
endTimestamp = "2014-01-30 08:05:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:00:00";
},
{
endTimestamp = "2014-01-30 08:10:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:05:00";
},
{
endTimestamp = "2014-01-30 08:15:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:10:00";
}
)
2014-01-28 08:39:42.825 Customer View[30896:70b] Start Time String: 2014-01-30 08:10:00
2014-01-28 08:39:42.826 Customer View[30896:70b] New Date: 2014-01-30 15:10:00 +0000
2014-01-28 08:39:42.826 Customer View[30896:70b] NEW NEW Date: 8:10 AM
2014-01-28 08:39:42.826 Customer View[30896:70b] collection Cell: <NSIndexPath: 0x906ddc0> {length = 2, path = 0 - 0}
2014-01-28 08:39:42.827 Customer View[30896:70b] Data: (
{
endTimestamp = "2014-01-30 08:05:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:00:00";
},
{
endTimestamp = "2014-01-30 08:10:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:05:00";
},
{
endTimestamp = "2014-01-30 08:15:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:10:00";
}
)
2014-01-28 08:39:42.827 Customer View[30896:70b] Start Time String: 2014-01-30 08:00:00
2014-01-28 08:39:42.827 Customer View[30896:70b] New Date: 2014-01-30 15:00:00 +0000
2014-01-28 08:39:42.828 Customer View[30896:70b] NEW NEW Date: 8:00 AM
2014-01-28 08:39:42.828 Customer View[30896:70b] collection Cell: <NSIndexPath: 0x906dde0> {length = 2, path = 0 - 1}
2014-01-28 08:39:42.828 Customer View[30896:70b] Data: (
{
endTimestamp = "2014-01-30 08:05:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:00:00";
},
{
endTimestamp = "2014-01-30 08:10:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:05:00";
},
{
endTimestamp = "2014-01-30 08:15:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:10:00";
}
)
2014-01-28 08:39:42.828 Customer View[30896:70b] Start Time String: 2014-01-30 08:05:00
2014-01-28 08:39:42.829 Customer View[30896:70b] New Date: 2014-01-30 15:05:00 +0000
2014-01-28 08:39:42.829 Customer View[30896:70b] NEW NEW Date: 8:05 AM
2014-01-28 08:39:42.829 Customer View[30896:70b] collection Cell: <NSIndexPath: 0x906de70> {length = 2, path = 0 - 2}
2014-01-28 08:39:42.830 Customer View[30896:70b] Data: (
{
endTimestamp = "2014-01-30 08:05:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:00:00";
},
{
endTimestamp = "2014-01-30 08:10:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:05:00";
},
{
endTimestamp = "2014-01-30 08:15:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:10:00";
}
)
2014-01-28 08:39:42.830 Customer View[30896:70b] Start Time String: 2014-01-30 08:10:00
2014-01-28 08:39:42.831 Customer View[30896:70b] New Date: 2014-01-30 15:10:00 +0000
2014-01-28 08:39:42.831 Customer View[30896:70b] NEW NEW Date: 8:10 AM
2014-01-28 08:39:42.831 Customer View[30896:70b] collection Cell: <NSIndexPath: 0x90677c0> {length = 2, path = 0 - 0}
2014-01-28 08:39:42.832 Customer View[30896:70b] Data: (
{
endTimestamp = "2014-01-30 08:05:00";
slotDuration = 5;
startTimestamp = "2014-01-30 08:00:00";
}
)
2014-01-28 08:39:42.832 Customer View[30896:70b] Start Time String: 2014-01-30 08:00:00
2014-01-28 08:39:42.832 Customer View[30896:70b] New Date: 2014-01-30 15:00:00 +0000
2014-01-28 08:39:42.833 Customer View[30896:70b] NEW NEW Date: 8:00 AM
截图:
应该是这样的
我的问题是,即使 numberOfItemsInSection
有时会返回 3,它也只会显示所有时间为上午 8:00 的第一个单元格。
在为 Collection View 填充单元格时,我这样做是正确的还是我做错了什么?
最佳答案
我看到了一些潜在的问题:
首先,它看起来像 collectionView:numberOfItemsInSection:
期望总是从 0...N 部分开始顺序调用,但事实并非如此,因此您的迭代器变量 collectionIndex
可能无法按预期工作。
其次,看起来您正在为 TableView 委托(delegate)方法中的 Collection View (collectionCellData
) 填充数据模型。如果 BJTableCeption
像 UITableView
一样工作,这也可能会导致问题,因为 cellForItem:
会被多次调用,导致 中有额外的对象collectionCellData
.
第三,在您的集合单元格出列之后,每次都会将标签添加到单元格中。因此,如果一个收集单元被重复使用,它将有重复的标签堆叠在一起。您应该创建一个自定义子类,仅在初始化时创建其 subview 。
我建议在重新加载集合单元之前填充一次 collectionCellData
数组(viewDidLoad
或 initWithNibName:bundle:
是放置的好地方您的设置代码)。在 cellForItem:
中,为每个 Collection View 的 tag
属性分配包含它的行;然后,在 collectionView:numberOfItemsInSection:
中使用标签代替 collectionIndex
。
关于ios - UICollectionView 不添加单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21410714/
我有一个并排有两列的布局。有没有一种简单的方法可以使用单个 UICollectionView 来做到这一点?唯一的要求是该解决方案必须适用于 iOS 8,并且单元格必须在每列中垂直堆叠,如下所示:
我使用标准 UICollectionView与部分。我的单元格像网格一样排列。如果用户使用 Apple TV Remote 移动焦点,则所选方向的下一个单元格会正确聚焦。但是如果网格中有“间隙”,默认
这是我的 ImageController 类(一个 ViewController)仅显示 Collection View 的背景,而其中的单元格则不显示。有什么帮助吗?是不是我没有正确初始化什么? 这
我希望能够在 UICollectionView 中设置内容大小的最小高度,这样我就可以隐藏/显示 UISearchbar,类似于在 iBooks 上完成的方式。 但是,我不想对布局进行子类化,因为我想
有没有办法禁用 UICollectionView 的自动滚动细胞何时聚焦?当单元格进入焦点时,我想手动调整单元格的内容偏移量。 我不想更新内容偏移量: - (void)didUpdateFocusIn
如何将 UICollectionView 中的所有单元格垂直和水平居中使用 UICollectionViewFlowLayout ? 流布局根据滚动方向在右侧或底部留下间距。我想在所有方面设置相同的填
所以这是一个很无聊的问题,但我只是想不出一种非常简单的方法来检测当前关注的项目的 indexPath . 我环顾四周,希望看到一些非常简单的东西,例如 collectionView.indexPath
每当 UICollectionView 完全加载时,我都必须执行一些操作,即此时应调用所有 UICollectionView 的数据源/布局方法。我怎么知道?是否有任何委托(delegate)方法可以
每当 UICollectionView 完全加载时,我都必须执行一些操作,即此时应调用所有 UICollectionView 的数据源/布局方法。我怎么知道?是否有任何委托(delegate)方法可以
所以,UICollectionView 现在对我来说真的很痛苦。假设我有一个 UIViewController ,其中嵌入了一个 UICollectionView 。 CollectionView 的
好的,如果我有 UIViewController如果我使用这个 `preferredFocusedView ,我可以制作一个专注于 tvOS 的 View ,但是如何让 UICollectionVie
我已经构建了 2 个 Collection View ,1 个水平滚动(在顶部)和 1 个垂直滚动(在中间 - 底部),用于在我的 Objective-C iOS 应用程序中查看 2 组不同的内容,类
这就是我的问题,您在 gif 上看到的 4 个橙色矩形是单个垂直 UICollectionView = OrangeCollectionView。 绿色和紫色“卡片” View 是另一个 UIColl
大家好,我是 IOS 开发的新手,我有一个 UICollection,我在其中使用 SDWebImageCache 加载图像。问题是我的 UICollectionview 单元格在快速滚动时感觉像抽搐
我在 ViewController 中有两个 Collection View 。需要在用户滚动底部 UICollectionView 时自动滚动顶部 collectionView。 最佳答案 你可以试
我正在研究 UICollectionView,我有一个在单元格中使用 collectionView 的想法?可以吗 当我运行项目时,我收到一个错误 "InterFace Builder StoryBo
大家好,我收到以下错误 -UICollectionView 必须使用非 nil 布局参数进行初始化 PopularShotsCollectionViewController 的代码: import U
我的应用程序出现此错误: *** -[UICollectionView _endItemAnimations] 断言失败,/SourceCache/UIKit/UIKit-2372/UICollect
当我的 UIViewController 与 UICollectionView 一起出现时,内容会向上滚动一点,出现时。 我实现了 scrollViewDidScroll:我正在记录 contentO
我遇到了一个问题,在我的 UICollectionView 中,当返回的记录为 0 时,我需要显示一个页脚。下面的代码似乎运行良好,当没有记录时,显示页脚。但是,有一个问题,当有记录时,虽然foote
我是一名优秀的程序员,十分优秀!