IOS UITableView (三)
2015-12-02 00:02
417 查看
import UIKit
class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
var tableView:UITableView?
var allnames:Dictionary<Int, [String]>?
var adHeaders:[String]?
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
//初始化数据,这一次数据,我们放在属性列表文件里
self.allnames = [
0:[String]([
"UILabel 标签",
"UITextField 文本框",
"UIButton 按钮"]),
1:[String]([
"UIDatePiker 日期选择器",
"UIToolbar 工具条",
"UITableView 表格视图"])
];
print(self.allnames)
self.adHeaders = [
"常见 UIKit 控件",
"高级 UIKit 控件"
]
//创建表视图
self.tableView = UITableView(frame:self.view.frame, style:UITableViewStyle.Grouped)
self.tableView!.delegate = self
self.tableView!.dataSource = self
//创建一个重用的单元格
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
self.view.addSubview(self.tableView!)
//创建表头标签
let 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
}
//在本例中,有2个分区
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}
//返回表格行数(也就是返回控件数)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let data = self.allnames?[section]
return data!.count
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
func tableView(tableView:UITableView, titleForHeaderInSection
section:Int)->String?
{
var headers = self.adHeaders!;
return headers[section];
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部
func tableView(tableView:UITableView, titleForFooterInSection
section:Int)->String?
{
let data = self.allnames?[section]
return "有\(data!.count)个控件"
}
//创建各单元显示内容(创建参数indexPath指定的单元)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell
{
//为了提供表格显示性能,已创建完成的单元需重复使用
let identify:String = "SwiftCell"
//同一形式的单元格重复使用,在声明时已注册
let secno = indexPath.section
let data = self.allnames?[secno]
if(secno == 0)
{
let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath)
as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
let image = UIImage(named:"xin.png")
cell.imageView?.image = image
cell.textLabel?.text = data![indexPath.row]
return cell
}
else
{
//第二个分组表格使用详细标签
let adcell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: "SwiftCell")
let image = UIImage(named:"xin.png")
adcell.imageView?.image = image
adcell.textLabel?.text = data![indexPath.row]
adcell.detailTextLabel!.text = "这是\(data![indexPath.row])的说明"
return adcell;
}
}
// UITableViewDelegate 方法,处理列表项的选中事件
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
let itemString = self.allnames![indexPath.section]![indexPath.row]
let alertview = UIAlertView();
alertview.title = "提示!"
alertview.message = "你选中了【\(itemString)】";
alertview.addButtonWithTitle("确定")
alertview.show();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
var tableView:UITableView?
var allnames:Dictionary<Int, [String]>?
var adHeaders:[String]?
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
//初始化数据,这一次数据,我们放在属性列表文件里
self.allnames = [
0:[String]([
"UILabel 标签",
"UITextField 文本框",
"UIButton 按钮"]),
1:[String]([
"UIDatePiker 日期选择器",
"UIToolbar 工具条",
"UITableView 表格视图"])
];
print(self.allnames)
self.adHeaders = [
"常见 UIKit 控件",
"高级 UIKit 控件"
]
//创建表视图
self.tableView = UITableView(frame:self.view.frame, style:UITableViewStyle.Grouped)
self.tableView!.delegate = self
self.tableView!.dataSource = self
//创建一个重用的单元格
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
self.view.addSubview(self.tableView!)
//创建表头标签
let 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
}
//在本例中,有2个分区
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}
//返回表格行数(也就是返回控件数)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let data = self.allnames?[section]
return data!.count
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
func tableView(tableView:UITableView, titleForHeaderInSection
section:Int)->String?
{
var headers = self.adHeaders!;
return headers[section];
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部
func tableView(tableView:UITableView, titleForFooterInSection
section:Int)->String?
{
let data = self.allnames?[section]
return "有\(data!.count)个控件"
}
//创建各单元显示内容(创建参数indexPath指定的单元)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell
{
//为了提供表格显示性能,已创建完成的单元需重复使用
let identify:String = "SwiftCell"
//同一形式的单元格重复使用,在声明时已注册
let secno = indexPath.section
let data = self.allnames?[secno]
if(secno == 0)
{
let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath)
as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
let image = UIImage(named:"xin.png")
cell.imageView?.image = image
cell.textLabel?.text = data![indexPath.row]
return cell
}
else
{
//第二个分组表格使用详细标签
let adcell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: "SwiftCell")
let image = UIImage(named:"xin.png")
adcell.imageView?.image = image
adcell.textLabel?.text = data![indexPath.row]
adcell.detailTextLabel!.text = "这是\(data![indexPath.row])的说明"
return adcell;
}
}
// UITableViewDelegate 方法,处理列表项的选中事件
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
let itemString = self.allnames![indexPath.section]![indexPath.row]
let alertview = UIAlertView();
alertview.title = "提示!"
alertview.message = "你选中了【\(itemString)】";
alertview.addButtonWithTitle("确定")
alertview.show();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
相关文章推荐
- iOS运用runtime全局修改UILabel的默认字体
- poj3481 Double Queue
- Vivado AXI Reference Guide 阅读笔记
- UIView的概念与使用
- IOS UITableView (二)
- pcduino 无法打开usb摄像头。
- PMON failed to acquire latch, see PMON dump
- UIAlertViewController 2
- IOS UITableView
- Queue、BlockingQueue以及利用BlockingQueue实现生产者/消费者模型
- UI基础_UIImage
- 【干货】jsMind思维导图整合Easyui的右键菜单
- Unique Binary Search Trees
- iOS-自定义 UITabBarController
- iOS-自定义 UITabBarController
- GCD 与 NSOperationQueue
- Long.ValueOf(“String”) Long.parseLong(“String”) 区别
- HDOJ 5504 GT and sequence (乱搞)
- 树莓派与Arduino Uno的对接(USB及GPIO方式)
- UI窗口尺寸计算