import
UIKit
class
ViewController
:
UIViewController
,
UITableViewDelegate
,
UITableViewDataSource
{
var
tableView:
UITableView
?
var
ctrlnames:[
String
] = [
"UILabel 标签"
,
"UIButton 按钮"
,
"UIDatePiker 日期选择器"
,
"UITableView 表格视图"
]
var
selectedCellIndexPath:
NSIndexPath
!
override
func
viewDidLoad() {
super
.viewDidLoad()
self
.tableView =
UITableView
(frame:
UIScreen
.mainScreen().applicationFrame,
style:
UITableViewStyle
.
Plain
)
self
.tableView!.delegate =
self
self
.tableView!.dataSource =
self
self
.tableView!.registerClass(
UITableViewCell
.
self
, forCellReuseIdentifier:
"SwiftCell"
)
self
.view.addSubview(
self
.tableView!)
}
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
{
var
label =
UILabel
(frame:
CGRectZero
)
label.setTranslatesAutoresizingMaskIntoConstraints(
false
)
label.text =
self
.ctrlnames[indexPath.row]
var
textview=
UITextView
(frame:
CGRectZero
)
textview.setTranslatesAutoresizingMaskIntoConstraints(
false
)
textview.textColor =
UIColor
.grayColor()
textview.text =
"UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,"
let
identify:
String
=
"SwiftCell"
var
cell =
UITableViewCell
(style:
UITableViewCellStyle
.
Default
, reuseIdentifier:identify)
cell.layer.masksToBounds =
true
cell.contentView.addSubview(label)
cell.contentView.addSubview(textview)
var
views:
NSMutableDictionary
=
NSMutableDictionary
()
views.setValue(label, forKey:
"label"
)
views.setValue(textview, forKey:
"textview"
)
cell.contentView.addConstraints(
NSLayoutConstraint
.constraintsWithVisualFormat(
"H:|-15-[label]-15-|"
, options:
nil
, metrics:
nil
, views: views))
cell.contentView.addConstraints(
NSLayoutConstraint
.constraintsWithVisualFormat(
"H:|-15-[textview]-15-|"
, options:
nil
, metrics:
nil
, views: views))
cell.contentView.addConstraints(
NSLayoutConstraint
.constraintsWithVisualFormat(
"V:|[label(40)]"
, options:
nil
, metrics:
nil
, views: views))
cell.contentView.addConstraints(
NSLayoutConstraint
.constraintsWithVisualFormat(
"V:|-40-[textview(80)]"
, options:
nil
, metrics:
nil
, views: views))
return
cell
}
func
tableView(tableView:
UITableView
!, didSelectRowAtIndexPath indexPath:
NSIndexPath
!)
{
self
.tableView!.deselectRowAtIndexPath(indexPath, animated:
false
)
selectedCellIndexPath = indexPath
tableView!.reloadRowsAtIndexPaths([indexPath],
withRowAnimation:
UITableViewRowAnimation
.
Automatic
)
}
func
tableView(tableView:
UITableView
,
heightForRowAtIndexPath indexPath:
NSIndexPath
) ->
CGFloat
{
if
(selectedCellIndexPath !=
nil
&& selectedCellIndexPath == indexPath){
return
120
}
return
40
}
}
请发表评论