0

I have three custom UITableView Cells and I am unable to reduce the spacing between the cells. I have tried to reference this post here however is not working on my specific case. I have three custom UITableViewCells however the spacing between them will not reduce. I am not sure what I am missing but I tried to adjust the size of the header as well and that did not help at all.

     private lazy var tableView: UITableView = {
    let tbl = UITableView(frame: .zero, style: .grouped)
    tbl.dataSource = self
    tbl.delegate = self
    tbl.register(UINib(nibName: cardCellId, bundle: nil), forCellReuseIdentifier: cardCellId)
    tbl.register(UINib(nibName: aprCellId, bundle: nil), forCellReuseIdentifier: aprCellId)
    tbl.register(UINib(nibName: transactionCellId, bundle: nil), forCellReuseIdentifier: transactionCellId)
    tbl.register(UINib(nibName: transactionHeaderId, bundle: nil), forHeaderFooterViewReuseIdentifier: transactionHeaderId)
    tbl.tableFooterView = UIView()
    tbl.separatorStyle = .none
    tbl.backgroundColor = UIColor(red: 225/255, green: 225/255, blue: 234/255, alpha: 1)
    return tbl
}()

private var cardCellId: String { return String(describing: CardDetailsCell.self) }
private var aprCellId: String { return String(describing: APRCell.self) }
private var transactionCellId: String { return String(describing: TransactionDetailCell.self) }
private var transactionHeaderId: String { return String(describing: TransactionHeaderView.self) }
private lazy var numberFormatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.maximumFractionDigits = 2
    return formatter
}()

private var detailType: DetailType = .creditCard



enum DetailType{
    case creditCard, checkingAccount,projectedInterest
}

init(card: CreditCard, env: PlaidEnv, accessToken: String,detailType:DetailType) {
    self.card = card
    self.env = env
    self.accessToken = accessToken
    self.detailType = detailType
    super.init(nibName: nil, bundle: nil)
}
init(checkingAccount: CheckingAccount, env: PlaidEnv, accessToken: String,detailType:DetailType) {
    self.checkingAccount = checkingAccount
    self.env = env
    self.accessToken = accessToken
    self.detailType = detailType
    super.init(nibName: nil, bundle: nil)
}
init(projectedInterest: WIPHomeViewController.SectionAccount, env: PlaidEnv, accessToken: String,detailType:DetailType) {
    self.projectedInterest = projectedInterest
    self.env = env
    self.accessToken = accessToken
    self.detailType = detailType
    super.init(nibName: nil, bundle: nil)
}

override func loadView() {
    view = tableView
}

func numberOfSections(in tableView: UITableView) -> Int {
    switch detailType {
    case .creditCard:
        return 3
    case .checkingAccount:
        return 2
    case .projectedInterest:
        return 1
    
    }
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0:
        if detailType == .projectedInterest {
            return transactions.count
        }else{
        return 1
        }
    case 1:
        switch detailType {
        case .creditCard:
            
            let expandedCount = 2 + ((card?.aprs.isEmpty)! ? 1 : card?.aprs.count)!
            return isExpandedAPR ? expandedCount : 3
               
            
        case .checkingAccount:
            return transactions.count
        default:
            return 0
        }
    case 2:
        return transactions.count
    default:
        return 0
    }
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier(forSection: indexPath.section), for: indexPath)
    if let aprCell = cell as? APRCell {
        aprCell.roundableTopConstraint.constant = 0
        aprCell.roundableBottomConstraint.constant = 0
        if indexPath.row == 0 {
            aprCell.roundableTopConstraint.constant = 20
        }
        if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
            aprCell.roundableBottomConstraint.constant = 20
        }
        configureAPRCell(aprCell, forRowAt: indexPath)
    }
    return cell
}

func cellIdentifier(forSection section: Int) -> String {
    guard section < 3 else { fatalError("No identifier for section") }
    switch detailType {
    case .creditCard:
        return [cardCellId, aprCellId, transactionCellId][section]
    case .checkingAccount:
        return [cardCellId,  transactionCellId][section]
    case .projectedInterest:
        return [  transactionCellId][section]
    }
    

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.selectionStyle = .none
    switch cell {
    case let cardCell as CardDetailsCell:
        configureCardCell(cardCell, forRowAt: indexPath)
    case let aprCell as APRCell:
        configureAPRCell(aprCell, forRowAt: indexPath)
    case let transactionCell as TransactionDetailCell:
        configureTransactionCell(transactionCell, forRowAt: indexPath)
    default:
        return
    }
}



func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    switch detailType {
    case .creditCard:
        guard section > 1 else { return nil }
        return tableView.dequeueReusableHeaderFooterView(withIdentifier: transactionHeaderId)
    case .checkingAccount:
        guard section > 0 else { return nil }
        return tableView.dequeueReusableHeaderFooterView(withIdentifier: transactionHeaderId)
    case .projectedInterest:
        return tableView.dequeueReusableHeaderFooterView(withIdentifier: transactionHeaderId)
    }
}





func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    switch detailType {
    case .creditCard:
        return section > 1 ? 60 : 0
    case .checkingAccount:
        return section > 0 ? 60 : 0
    case .projectedInterest:
        return 60
        
    }
    
}

   func configureCardCell(_ cardCell: CardDetailsCell, forRowAt indexPath: IndexPath) {
    if let card = self.card {
        cardCell.institutionLabel.text = card.institution
        cardCell.balanceLabel.text = "$\(numberFormatter.string(from: NSNumber(value: card.balance)) ?? "")"
        cardCell.numberLabel.text = "**** **** **** \(card.mask ?? "N/A")"
        let dueNumber = NSNumber(value: card.amountDue ?? 0)
        cardCell.dueLabel.text = "$\(numberFormatter.string(from: dueNumber) ?? "") due"
        guard let limit = card.limit, limit > 0 else {
            cardCell.utilizationLabel.text = "N/A"
            return
        }
        let utilizationNumber = NSNumber(value: card.balance/limit*100)
        cardCell.utilizationLabel.text = "\(numberFormatter.string(from: utilizationNumber) ?? "")%"
    }
    else if let checkingAccount = self.checkingAccount{
        cardCell.dueLabel.isHidden = true
        cardCell.utilizationLabel.isHidden = true
        cardCell.utilizationLabelHeading.isHidden = true
        cardCell.institutionLabel.text = checkingAccount.institution
        cardCell.balanceLabel.text = "$\(numberFormatter.string(from: NSNumber(value: checkingAccount.balance )) ?? "")"
        cardCell.numberLabel.text = "**** **** **** \(checkingAccount.mask ?? "N/A")"
    }
}

func configureAPRCell(_ cell: APRCell, forRowAt indexPath: IndexPath) {
    cell.subvalueLabel.text = ""
   
   
    switch indexPath.row {
    case 0:
        cell.roundsTopCorners = true
        cell.roundsBottomCorners = false
        cell.hidesSeparator = false
        cell.titleLabel.text = "Due Date"
        cell.subtitles = ["For miminum payment"]
        cell.valueLabel.text = card?.dueDate.plaidOrdinalDay
        cell.icon.image = UIImage.dueIcon
     //   cell.anchor(top: nil, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 5, paddingBottom: 0, paddingRight: 5, width: 0, height: 60)
    case 1:
        cell.roundsTopCorners = false
        cell.roundsBottomCorners = false
        cell.hidesSeparator = false
        cell.titleLabel.text = "Statement Date"
        cell.subtitles = ["For billing period"]
        cell.valueLabel.text = card?.statementDate?.plaidOrdinalDay
        cell.icon.image = UIImage.dueIcon
    default:
        cell.roundsTopCorners = false
        cell.roundsBottomCorners = tableView.numberOfRows(inSection: 1) == indexPath.row + 1
        cell.hidesSeparator = true
        cell.titleLabel.text = "Yearly interest %"
        cell.subtitles = ["For purchases"]
        cell.valueLabel.text = "0 %"
        cell.icon.image = UIImage.percentageIcon
        if let card = self.card {
        guard !card.aprs.isEmpty else { return }
        let apr = card.aprs[indexPath.row - 2]
        cell.subtitles = [apr.aprTypeDescription]
        let numberAPR = NSNumber(value: apr.apr_percentage)
        cell.valueLabel.text = "\(numberFormatter.string(from: numberAPR) ?? "")%"
        }
    }
}

func configureTransactionCell(_ cell: TransactionDetailCell, forRowAt indexPath: IndexPath) {
   // cell.iconImageView.image = UIImage.groceriesIcon
    let transaction = transactions[indexPath.row]

    let name = "..."
    
    cell.titleLabel.text = (transaction.name.firstCharacterUpperCase()?.maxLength(length: 15))! + name
    cell.valueLabel.font = UIFont(name: FontName.poppinsSemiBold.rawValue, size: 14)

   // self.tableView.rowHeight = 200

    cell.backgroundColor = .debtlyOffGreyBackground
    
    let spentLabel = UILabel.createPoppinsLabel(text: .empty, size: 14.0, font: .poppinsSemiBold, color: .debtlyBlack)
    cell.subtitleLabel.text = "\(transaction.location.city ?? "") \(transaction.date)"
 
    
    
    
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.minimumFractionDigits = 2
    formatter.maximumFractionDigits = 2
  //  guard let num = formatter.string(for: transaction.amount) else {return}
    
    var numABS = abs(transaction.amount)
    
    guard let num = formatter.string(for: numABS) else {return}

    cell.valueLabel.text = "$\(num)"
    if let iconTitle = transaction.iconTitle{
        cell.icon.image = getTransactionIcon(title: iconTitle)
    }else{
        cell.icon.image = getTransactionIcon(title: "")
        loadTransIcon(transId: transaction.transaction_id)
    }
    
    if self.greenColorTransactionIds.contains(transaction.category_id){
        cell.valueLabel.textColor = hexStringToUIColor(hex: "#49CD94")

      //  cell.titleLabel.textColor = hexStringToUIColor(hex: "#49CD94")
    }else{
        cell.titleLabel.textColor = hexStringToUIColor(hex: "#5A5A5A")
        cell.valueLabel.textColor = hexStringToUIColor(hex: "#5A5A5A")

    }
    
}
G Gara
  • 1

0 Answers0