UITableView swift
//
// ViewController.swift
// UILabelTest
//
// Created by mac on 15/6/23.
// Copyright (c) 2015年 fangyuhao. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
var ctrlnames:[String]?
var tableView:UITableView?
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
//初始化数据
self.ctrlnames = NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("Controls", ofType: "plist")!) as Array
println(self.ctrlnames)
//创建表视图
self.tableView = UITableView(frame: self.view.frame, style: UITableViewStyle.Plain)
self.tableView!.delegate = self
self.tableView!.dataSource = self
//创建一个重用的单元格
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
self.view.addSubview(self.tableView!)
//创建表头标签
var headerLabel = UILabel(frame: CGRectMake(0, 0, self.view.bounds.size.width, 30))
headerLabel.backgroundColor = UIColor.blackColor()
headerLabel.textColor = UIColor.whiteColor()
headerLabel.numberOfLines = 0
headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
headerLabel.text = "常见UIKit控件"
headerLabel.font = UIFont.italicSystemFontOfSize(20)
self.tableView!.tableHeaderView = headerLabel
}
//在本例中,只有一个分区
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//返回表格行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.ctrlnames!.count
}
//创建各单元显示内容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//为了提供表格显示性能,已创建的单元需要重复使用
let identify:String = "SwiftCell"
//
let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.textLabel?.text = self.ctrlnames![indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView?.deselectRowAtIndexPath(indexPath, animated: true)
var itemString = self.ctrlnames![indexPath.row]
var alertview = UIAlertView()
alertview.title = "提示!"
alertview.message = "你选中了[\(itemString)]"
alertview.addButtonWithTitle("YES")
alertview.show()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}