gpt4 book ai didi

ios - UICollectionView 不添加单元格

转载 作者:行者123 更新时间:2023-11-28 20:07:54 25 4
gpt4 key购买 nike

我有一个包含 UICollectionViewUITableViewCell。每个 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

截图: enter image description here

应该是这样的

enter image description here

我的问题是,即使 numberOfItemsInSection 有时会返回 3,它也只会显示所有时间为上午 8:00 的第一个单元格。

在为 Collection View 填充单元格时,我这样做是正确的还是我做错了什么?

最佳答案

我看到了一些潜在的问题:

首先,它看起来像 collectionView:numberOfItemsInSection: 期望总是从 0...N 部分开始顺序调用,但事实并非如此,因此您的迭代器变量 collectionIndex 可能无法按预期工作。

其次,看起来您正在为 TableView 委托(delegate)方法中的 Collection View (collectionCellData) 填充数据模型。如果 BJTableCeptionUITableView 一样工作,这也可能会导致问题,因为 cellForItem: 会被多次调用,导致 中有额外的对象collectionCellData.

第三,在您的集合单元格出列之后,每次都会将标签添加到单元格中。因此,如果一个收集单元被重复使用,它将有重复的标签堆叠在一起。您应该创建一个自定义子类,仅在初始化时创建其 subview 。

我建议在重新加载集合单元之前填充一次 collectionCellData 数组(viewDidLoadinitWithNibName:bundle: 是放置的好地方您的设置代码)。在 cellForItem: 中,为每个 Collection View 的 tag 属性分配包含它的行;然后,在 collectionView:numberOfItemsInSection: 中使用标签代替 collectionIndex

关于ios - UICollectionView 不添加单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21410714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com