简体   繁体   English

根据其类型具有不同图像的单元格:UICollectionView

[英]Cells with different images based on their types: UICollectionView

I am trying to set different images on cells based on their types. 我试图根据其类型在单元格上设置不同的图像。 Each section has different types of cells. 每个部分都有不同类型的单元格。 At least two types, like there are seats in seat map so legends are available seats, booked, disabled seat and selected seat. 至少有两种类型,例如座位图上有座位,因此图例是可用座位,已预订座位,残疾人座位和选定座位。 What i am not getting is how to identify cells and assign them images accordingly. 我没有得到的是如何识别细胞并相应地为其分配图像。 I am getting total seats plus disabled seats. 我正在获得总座位数和残疾人士座位数。 Here disabled seats are for example i have 20 total seats and in a row i am showing 9 seats so there will be three rows generated having 9,9 and 2 seats, i have completed it to 27 so that all three rows get filled with seats, those 7 extra seats are disabled seats. 例如,这里有残障席,我总共有20个席位,连续显示9个席位,因此将生成三排,分别有9,9和2个席位,我已经将其完成到27个,所以所有三排都坐满了,这7个额外的座位是残疾人座位。 How can i manage these now. 我现在如何管理这些。 Feel free to ask if anything not clear. 随意问是否有不清楚的地方。

This is what i have tried so far. 到目前为止,这是我尝试过的。

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(@"vals count:%d", [arrSeats count]);
for(int i=0; i<[arrLevels count]; i++)
{
    if(section == i)
    {
        int v;
        NSString *cnt = [NSString stringWithFormat:@"%@",[arrTot objectAtIndex:i]];
        v = [cnt intValue];
        if(v<=9)
        {
            return 9;
        }
        else{
       int numOfRow = v/9; //2
        if(v % 9 > 0)
            numOfRow +=1;  //2+1 = 3
       int c = numOfRow*9;
        return c;
        } 
    }
}
return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

int v;
NSString *cnt = [NSString stringWithFormat:@"%@",[arrTot objectAtIndex:indexPath.section]];
v = [cnt intValue];
if(v < 9)
{
    if(cell.selected){
        cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow_seat.png"]];  // highlight selection
    }
    else
    {
        cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_s.png"]]; // Default color
    }
}else{
    int numOfRow = v/9; //2
    if(v % 9 > 0)
        numOfRow +=1;  //2+1 = 3
    int c = numOfRow*9;
    int get = c-v;
    for(int i=0; i<get; i++)
    {
    cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blank_seat.png"]];
    }
}

return cell;
}

UIColoectionView we get indexPath which gives section and item number of cell. UIColoectionView我们得到indexPath,它给出了单元格的部分和项目编号。 So check indexPath.row. 因此,检查indexPath.row。 If less then 20 check cell.selected. 如果少于20,则选中单元格。 If greater then 20 they are blank. 如果大于20,则为空白。 I wrote some code. 我写了一些代码。 Please check correct logic 请检查正确的逻辑

int v;
NSString *cnt = [NSString stringWithFormat:@"%@",[arrTot objectAtIndex:indexPath.section]];
v = [cnt intValue];  
if(indexPath.row < v)
{


if(v < 9)
{
    if(cell.selected){
        cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow_seat.png"]];  // highlight selection
    }
    else
    {
        cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_s.png"]]; // Default color
    }
}

}
else
{
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blank_seat.png"]];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM