У меня есть UITableViewCell
, который содержит UICollectionView
. Каждый вид коллекции может иметь максимум 3 ячейки. Я добавляю ячейки с этим кодом.UICollectionView не добавляя ячейки
- (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 утра для всех.
Я делаю это правильно или я делаю что-то неправильно, когда дело доходит до заполнения ячеек для просмотра коллекции?
Является ли мой registerClass правильным в cellForItem? – Bot
Да, это нормально, чтобы регистрировать классы с видом коллекции; даже если вы перерегистрируете класс/ниб для идентификатора повторного использования, он просто заменит старую запись. – Austin
Хорошо, спасибо за помощь. Я использовал ваш подход, но вместо использования тега я создал подкласс и сохранил массив данных в переменной, а затем просто получил доступ к данным из этого набора. Я также переключил registerClass на registerNib и сделал свою ячейку в xib, так что мне не нужно создавать и добавлять ярлык. – Bot