G
G
Gleb2021-01-09 16:22:23
Swift
Gleb, 2021-01-09 16:22:23

How to address 0 and 1 element, skipping the rest?

I want to figure out how it is possible to pass only the zero and first element, instead of all?
tried to make a loop
for IndexPath in 1 { ...}
got an error For-in loop requires 'Int' to conform to 'Sequence'
Any advice on what to do?
here is the function:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = mainView.collectionView.dequeueReusableCell(withReuseIdentifier: MainCollectionViewCell.cellId, for: indexPath) as! MainCollectionViewCell
        
        cell.titlelabel.text = fetchedDataArray?[indexPath.row].title
        cell.textLabel.text = fetchedDataArray?[indexPath.row].listDescription
        cell.priceLabel.text = fetchedDataArray?[indexPath.row].price
        cell.titleImageView.image = images?[indexPath.row]
        
        switch indexPath {
        case selectedIndexPath:
            cell.checkMarkButton.isHidden = false
            guard let cellTitleText = cell.titlelabel.text else {
                return UICollectionViewCell()
                
            }
            selectedTitleString = cellTitleText
            
        default:
            cell.checkMarkButton.isHidden = true
        }
        return cell
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
briahas, 2021-01-09
@shevzoom

"For-in loop" is for iterating over Sequences. You put instead of Sequence - number. Instead of a number, put a Sequence of the IndexPaths you need, and get what you need.
updated:
if you don't want only, always, and only the third element then :

  • collect another array that does not contain this element
  • or inside the loop body put a check if this is the third element then continue

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question