效果圖如下:
用xib創(chuàng)建的cell , 請看如下代碼:
<pre>
class HomePageViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var tableView:UITableView!
var dataArray = NSMutableArray()
var imageArray = NSMutableArray()
let cellID = "testCellID"
var isEdit = false //判斷tabview是否在編輯狀態(tài)
override func viewDidLoad() {
super.viewDidLoad()
self.title = "首頁"
self.dataArray = ["BIGBANG","G-DRAGON","TOP","DAESUNG","SEUNGRI","TAEYANG"]
self.imageArray = ["bigbang","gd","top","daesung","seungri","taeyang"]
// self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "編輯", style: UIBarButtonItemStyle.done, target: self, action:#selector(HomePageViewController.rightBarButtonItemClicked))
//初始化TableView
self.tableView = UITableView(frame:self.view.frame,style:.plain)
self.tableView.delegate = self
self.tableView.dataSource = self
//創(chuàng)建單元格
self.tableView!.register(UINib(nibName:"XIBTableViewCell",bundle:nil), forCellReuseIdentifier: "XIBTableViewCell")
// self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: cellID)
self.view.addSubview(self.tableView)
//去掉沒有數(shù)據(jù)的cell的分割線
self.tableView.tableFooterView = UIView()
}
//導(dǎo)航欄右側(cè)按鈕事件
func rightBarButtonItemClicked(){
if isEdit{
self.tableView.setEditing(false, animated: true)
isEdit = false
}else{
self.tableView.setEditing(true, animated: true)
isEdit = true
}
}
//cell內(nèi)容的顯示
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:XIBTableViewCell = tableView.dequeueReusableCell(withIdentifier: "XIBTableViewCell", for: indexPath) as! XIBTableViewCell
cell.cellLabel!.text = self.dataArray[indexPath.row] as? String
cell.cellImage?.image = UIImage(named:self.imageArray[indexPath.row] as! String)
return cell
}
//返回的cell的行數(shù)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataArray.count
}
//單元格高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
}
</pre>