從零開始學(xué)Swift之UITableView

本文提供一個基于Storyboard的UITableView的用法,比較適合新手體驗從數(shù)組的數(shù)據(jù)放入到UITableView的TableCell里面

第一步

在Storyboard里面,將TableView拉入到View里面,然后在Table View里面添加Table Cell 最后根據(jù)自己的情況添加其他組件,這里我添加的是label

view

第二步

綁定tableview的dataSource 和 datagate 到viewController,如圖所示

綁定tableview

第三步

新建一個ViewTableCell.swift的文件,并且繼承UITableViewCell。最后在Storyboard中,把第一步添加的label綁定到這個UITableViewCell類中

綁定到Cell類中

第四步

要設(shè)置table Cell的identifier,我這里是設(shè)置成"tableCell",在下面的ViewController里面的實現(xiàn)方法需要用到

identifier

第五步

進(jìn)入ViewController類中,繼承UIViewController,UITableViewDelegate,UITableViewDataSource,然后需要實現(xiàn)2個方法

//
//  MainViewController.swift
//  Project02-CustomFonts
//
//  Created by space on 2018/4/20.
//  Copyright ? 2018年 space. All rights reserved.
//

import UIKit

class MainViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    
    var data = ["space","使用UITableView","使用UITableViewDelegate","1234567","abcdef","##$%^^&&","910455361@qq.com",
                    "151250070","xxxx@qq.com"]
    
    @IBOutlet weak var changeFontButton: UIButton!
    @IBOutlet weak var fontTableView: UITableView!
    
    
    /// 這里是返回tableView的行數(shù)
    ///
    /// - Parameters:
    ///   - tableView:
    ///   - section:
    /// - Returns: tableView的行數(shù)
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    /// 先添加tableview綁定到ViewController里,然后在ViewDidload里調(diào)用網(wǎng)絡(luò)鏈接獲取到網(wǎng)絡(luò)數(shù)據(jù),然后保存到一個類數(shù)組里,然后再加載完數(shù)據(jù)后調(diào)用綁定的
    ///
    /// - Parameters:
    ///   - tableView:
    ///   - indexPath:
    /// - Returns:
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = fontTableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! ViewTableCell
        let text = data[indexPath.row]
//        print(text)
        
        cell.textLabel?.text = text
//        cell.textLabel?.textColor = UIColor.black
        
        return cell
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        fontTableView.dataSource = self
        changeFontButton.layer.cornerRadius = 50

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


最后就可以運行程序了,實驗結(jié)果


運行結(jié)果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容