IOS: Custom Cell

本篇主要介绍如何在一个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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值