如何解决自定义 UITableView 单元格间距问题
我有三个自定义 UITableView 单元格,但无法缩小单元格之间的间距。我曾尝试参考 this post here 但不适用于我的具体案例。我有三个自定义 UITableViewCells 但是它们之间的间距不会减少。我不确定我错过了什么,但我也尝试调整标题的大小,但根本没有帮助。
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,forCellReuseIdentifier: aprCellId)
tbl.register(UINib(nibName: transactionCellId,forCellReuseIdentifier: transactionCellId)
tbl.register(UINib(nibName: transactionHeaderId,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,detailType:DetailType) {
self.checkingAccount = checkingAccount
self.env = env
self.accesstoken = accesstoken
self.detailType = detailType
super.init(nibName: nil,bundle: nil)
}
init(projectedInterest: WIPHomeViewController.SectionAccount,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")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。