Swift 3 – some snippets..

  • if let paypal = user.paypal {
    // paypal != nil
    } else {
    // paypal == nil
    }
  • guard let paypal = user.paypal else {
    // paypal == nil
    return
    }
  • Handler :
    • VC 1: (CashoutContentVC)

      var loadTotalBalanceHandler: (()->())?  

      if (condition) {

                           if let callback = self.loadTotalBalanceHandler {

                              callback()

                         }
      }

    • VC 2:

      let cashoutVC = storyboard.instantiateViewController(withIdentifier: “CASH OUT”) as! CashoutContentVC

              cashoutVC.loadTotalBalanceHandler = {[weak self] in

                  self?.loadTotalBalance() //things need to do when callback

              }

      //example 2 :  pass a Double from vc1 -> vc2 

    • VC1:
      var
      ratingScoreHandler: ((_ score: Double) -> ())?

      if (condition) {

                     if let callback = self.ratingScoreHandler {

                          callback(rating)

                      }
      }

    • VC 2:

      let ratingVC = RatingVC(nibName: “RatingVC”, bundle: nil)

      ratingVC.ratingScoreHandler = { [weak self] (score) in

                  if let strongSelf = self {

                      strongSelf.ratingScore = Int(score) //things need to do when callback

                  }

              }

Advertisements

UICollectionView set num of colums

I implemented UICollectionViewDelegateFlowLayout on my UICollectionViewController and override the method responsible for determining the size of the Cell. I then took the screen width and divided it with my column requirement. For example, I wanted to have 3 columns on each screen size. So here’s what my code looks like –

– (CGSize)collectionView:(UICollectionView *)collectionView

                  layout:(UICollectionViewLayout *)collectionViewLayout

  sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    CGRect screenRect = [[UIScreen mainScreen] bounds];

    CGFloat screenWidth = screenRect.size.width;

    float cellWidth = screenWidth / 3.0; //Replace the divisor with the column count requirement. Make sure to have it in float.

    CGSize size = CGSizeMake(cellWidth, cellWidth);

    return size;

}