本文提供一個基于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é)果