本篇主要介绍如何在一个tableview加入不同的custom tableview cell。首先是UITableviewController:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var transactionTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
transactionTableView.register(UINib(nibName: "TransactionCell", bundle: nil), forCellReuseIdentifier: "transactionCell")
transactionTableView.register(UINib(nibName: "PresentBalanceCell", bundle: nil), forCellReuseIdentifier: "presentBalanceCell")
transactionTableView.register(UINib(nibName: "DonateAndTransferCell", bundle: nil), forCellReuseIdentifier: "donateAndTransferCell")
}
}
extension ViewController: UITableViewDelegate {
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1:
return 1
case 2:
return 1
case 3:
return 2
default:
return 0
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 4
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return nil
case 1:
return nil
case 2:
return "Pending Transactions"
case 3:
return "Recent Transactions"
default:
return nil
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let presentBalanceCell = tableView.dequeueReusableCell(withIdentifier: "presentBalanceCell", for: indexPath) as! PresentBalanceCell
presentBalanceCell.presentAmountLabel.text = "$37.82"
presentBalanceCell.availableTransactionAmountLabel.text = "$35.72"
presentBalanceCell.pendingAmountLabel.text = "$2.10"
return presentBalanceCell
case 1:
let donateAndTransferCell = tableView.dequeueReusableCell(withIdentifier: "donateAndTransferCell", for: indexPath)
return donateAndTransferCell
case 2:
let pendingTransactionCell = tableView.dequeueReusableCell(withIdentifier: "transactionCell", for: indexPath) as! TransactionCell
pendingTransactionCell.dateLabel.text = "7/12/2018"
pendingTransactionCell.descriptionLabel.text = "Walmart give you money"
pendingTransactionCell.amountLabel.text = "$2.10"
pendingTransactionCell.statusLabel.text = "Pending"
return pendingTransactionCell
case 3:
let recentTransactionCell = tableView.dequeueReusableCell(withIdentifier: "transactionCell", for: indexPath) as! TransactionCell
if indexPath.row == 0 {
recentTransactionCell.dateLabel.text = "7/11/2018"
recentTransactionCell.descriptionLabel.text = "Your transaction failed"
recentTransactionCell.amountLabel.text = "$1.88"
recentTransactionCell.statusLabel.text = "Failed"
} else {
recentTransactionCell.dateLabel.text = "7/11/2018"
recentTransactionCell.descriptionLabel.text = "Exxon gave you cash back"
recentTransactionCell.amountLabel.text = "$0.34"
recentTransactionCell.statusLabel.text = ""
}
return recentTransactionCell
default:
return UITableViewCell()
}
}
}
在这一个tableview里面,我加入了三个不同的tableviewcell,在tableviewdelegate里的第一个function, numberOfRowsInSection, 对应的是每一个cell有多少个section。第二个function,numberOfSections, 用来对应一共有多少个section。第三个function,titleForHeaderInSection,用来给每一个上面section加上title。最后一个function,cellForRowAt,用来对应每一个section都是那个tableviewcell。
做出来大概是这个样子,这里我没有去匹配每一个label里的文字。
需要注意的是每一个tableviewcell在一开始都需要register,
transactionTableView.register(UINib(nibName: "DonateAndTransferCell", bundle: nil), forCellReuseIdentifier: "donateAndTransferCell")
在使用的时候,也需要用加dequeReusablecell关键字
let pendingTransactionCell = tableView.dequeueReusableCell(withIdentifier: "transactionCell", for: indexPath) as! TransactionCell