iOS UITableView的基础用法(swift版本)

340 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

已经有一段时间没有写过UITableView相关的代码了,流程有点遗忘了,今天正好有个小功能需要去做,就临时写了一遍,发现没有忘记具体的写法,写起来行云流水,代码贴出来,供初学者参考一下吧。

import UIKit

class RemoveAccountVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var mTableView: UITableView!
    
    private var mVM = RemoveAccountVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
    }
    
    private func initView() {
        mVM.initTableView(mTableView)
        mTableView.dataSource = self
        mTableView.delegate = self
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mVM.rowCount()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: RemoveAccountVM.cellIdentifier, for: indexPath) as? RemoveAccountCell
        cell?.bind(mVM.item(indexPath.row))
        return cell ?? UITableViewCell()
    }
}

class RemoveAccountVM {
    
    static let cellIdentifier = "RemoveAccountCell"
    
    private var mItems = [RemoveItem]()
    
    init() {
        mItems.append(RemoveItem(title: "账号财产已结清", info: "没有资产、欠款、未结清的资金和虚拟权益\n本账号通过本账号接入的第三方中没有未完成或存在争议的服务"))
        mItems.append(RemoveItem(title: "账号处于安全状态", info: "最近一个月内无绑定手机、修改手机号、找回密码、修改密码等操作\n账号为正常使用中且1年内无任何账号被限制的记录"))
    }
    
    func initTableView(_ tableView:UITableView) {
        tableView.register(UINib(nibName: RemoveAccountVM.cellIdentifier, bundle: nil), forCellReuseIdentifier: RemoveAccountVM.cellIdentifier)
    }
    
    func rowCount() -> Int {
        return mItems.count
    }
    
    func item(_ index:Int) -> RemoveItem? {
        if index < rowCount() {
            return mItems[index]
        }
        return nil
    }
}


struct RemoveItem {
    var title:String?
    var info:String?
}

哦对了,上面只是贴出了swift代码没有布局相关的,我做布局是用的Storyboard来写的,主体布局是一个UITableview然后是使用的自动布局,具体要布局成什么样子可以自行决定,然后就是Storyboard和swift写的UIViewController中定义的@IBOutlet weak var mTableView: UITableView!连线,然后通过代码设置数据代理和UITableView的事件代理,实现数据代理就可以显示出来了,很简单的,UITableviewCell的布局也很简单做了一下,也是用自动布局写的,新建的时候可以选择同swift类一块建立xib的布局文件。

import UIKit

class RemoveAccountCell: UITableViewCell {

    @IBOutlet weak var mTitle: UILabel!
    
    @IBOutlet weak var mInfo: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.selectionStyle = .none
    }
    
    func bind(_ item: RemoveItem?) {
        mTitle.text = item?.title
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 6.0
        let attributeString = NSMutableAttributedString(string: item?.info ?? "")
        attributeString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: (item?.info ?? "").count))
        mInfo.attributedText = attributeString
    }
}

UITableViewCell的bind方法中用到了NSMutableAttributedString富文本写法是为了给描述文字为多行时添加行间距,如果不了解的可以自己建立示例去亲自跑一下就知道什么意思了。

作为一个写代码的人,这手一定要经常练才行,不练起来等到具体要用的时候发现不会写了就尴尬了。写代码的兄弟们都动起来吧!