如何根据 UILabel 的内容从 UICollectionView 中隐藏特定单元格?



我对objective-c相当陌生,我目前正在尝试为我的投资组合构建一些应用程序,因此任何帮助将不胜感激!

到目前为止,我已经构建了一个带有自定义UICollectionViewCell类的UICollectionView。UICollectionView 的内容由 6 个不同的数组(相对于事件的不同类别(管理。

我想实现一个按钮,该按钮将根据日期(某种过滤(优化我当前的UICollectionView。

我希望创建一个 if 函数?如果与日期数组关联的 UILabel 与特定日期匹配,则隐藏单元格?

下面是我的代码,它初始化我的数组并从我拥有的字典类中添加内容。

UICollectionView.h

- (void)viewDidLoad {
[super viewDidLoad];
self.eventTitleArray = [[NSMutableArray alloc]initWithCapacity:8];
self.eventLocationArray = [[NSMutableArray alloc]initWithCapacity:8];
self.eventIconArray = [[NSMutableArray alloc]init];
self.eventPriceArray = [[NSMutableArray alloc]initWithCapacity:8];
self.eventTypeArray = [[NSMutableArray alloc]initWithCapacity:8];
self.eventDayArray = [[NSMutableArray alloc]initWithCapacity:10];
for (NSUInteger index = 0; (index < 8) ; index++){
EventsList *eventList = [[EventsList alloc] initWithIndex:index];
NSString *individualEventTitle = eventList.eventTitle;
NSString *individualEventLocation = eventList.eventLocation;
NSString *individualEventIcon = eventList.eventIcon;
NSString *individualEventPrice = eventList.eventPrice;
NSString *individualEventType = eventList.eventType;
NSArray *eventDays = eventList.eventDay;

[self.eventTitleArray addObject:individualEventTitle];
[self.eventLocationArray addObject:individualEventLocation];
[self.eventIconArray addObject:individualEventIcon];
[self.eventPriceArray addObject:individualEventPrice];
[self.eventTypeArray addObject:individualEventType];
[self.eventDayArray addObjectsFromArray:eventDays];
}
}

下面是我将数组分配给相应单元格 uilabels/uiimage 的代码

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
EventsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"eventsCell" forIndexPath:indexPath];
cell.eventImage.image = [self.eventIconArray objectAtIndex:indexPath.row];
cell.eventTitle.text = [self.eventTitleArray objectAtIndex:indexPath.row];
cell.eventLocation.text = [self.eventLocationArray objectAtIndex:indexPath.row];
cell.eventPrice.text = [self.eventPriceArray objectAtIndex:indexPath.row];
cell.eventType.text = [self.eventTypeArray objectAtIndex:indexPath.row];
return cell;
}

谢谢。

你应该过滤你的数组,例如eventTitleArray

eventTitleArray = [eventTitleArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id object, NSDictionary *bindings) {
return [object shouldIKeepYou];  // Return YES for each object you want in filteredArray.
}]];

然后,您可以通过执行以下操作重新加载集合视图

[collectionView reloadData]

创建一个array with bool并标记要隐藏的单元格。开始时,为所有行添加 NO 表示所有单元格都可见。

NSMutableArray * arrayForBool = [[NSMutableArray alloc]init];
for(int i=0; i< numberofRowCount; i++){
[arrayForBool addObject:[NSNumber numberWithBool:NO]];
}

根据 UILabel 的内容,将布尔值更改为"是"以隐藏单元格。

if(arrayForBool[indexpath]){
// hide the cell
}else{
// show the cell
}

最新更新