import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableData = [["title":"Swift - 让标签栏按钮UITabBarItem图片居中","image":"img1.png"],
["title":"Swift - 使用SSZipArchive实现文件的压缩、解压缩","image":"img2.png"],
["title":"Swift - 使用LINQ操作数组/集合","image":"img3.png"],
["title":"Swift - 给表格UITableView添加索引功能","image":"img4.png"],
["title":"Swift - 列表项尾部附件点击响应","image":"img5.png"],
["title":"Swift - 自由调整图标按钮中的图标和文字位置","image":"img6.png"]]
var tableView:UITableView?
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
//创建表视图
self.tableView = UITableView(frame: UIScreen.mainScreen().applicationFrame,
style:.Plain)
self.tableView!.delegate = self
self.tableView!.dataSource = self
//设置表格背景色
self.tableView!.backgroundColor = UIColor(red: 0xf0/255, green: 0xf0/255,
blue: 0xf0/255, alpha: 1)
//去除单元格分隔线
self.tableView!.separatorStyle = .None
//创建一个重用的单元格
self.tableView!.registerNib(UINib(nibName:"MyTableViewCell", bundle:nil),
forCellReuseIdentifier:"myCell")
self.view.addSubview(self.tableView!)
}
//在本例中,只有一个分区
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1;
}
//返回表格行数(也就是返回控件数)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
//单元格高度
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)
-> CGFloat {
return 100
}
//创建各单元显示内容(创建参数indexPath指定的单元)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell
{
let cell:MyTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell")
as! MyTableViewCell
let item = tableData[indexPath.row]
cell.customLabel.text = item["title"]
cell.customImage.image = UIImage(named:item["image"]!)
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
|