swift tableViewCell自定义
// // TableViewCell.swift // 练习 // Created by 刘阜澎 on 15/9/8. // Copyright (c) 2015年 刘阜澎. All rights reserved. // import UIKit protocol TableViewCellDelegate { // protocoldefinition goes here func buttonAction(sender:UIButton) } class TableViewCell: UITableViewCell { var delegate:TableViewCellDelegate?; var _button:UIButton!;
override init(style: UITableViewCellStyle,reuseIdentifier: String?) { super.init(style: style,reuseIdentifier: reuseIdentifier) self.selectionStyle=UITableViewCellSelectionStyle.None self.creatUI() } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func creatUI() { _button=UIButton.buttonWithType(UIButtonType.Custom) as! UIButton _button!.frame=CGRectMake(self.contentView.bounds.width-100,5,80,30) _button!.addTarget(self,action: "buttonAction:",forControlEvents: UIControlEvents.TouchUpInside) _button.setTitle("确定",forState: UIControlState.Normal) _button.setTitleColor(UIColor.orangeColor(),forState: UIControlState.Normal) self.contentView .addSubview(_button!)
} func buttonAction(sender:UIButton) { self.delegate!.buttonAction(sender) }
override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool,animated: Bool) { super.setSelected(selected,animated: animated) // Configure the view for the selected state }
} // ViewController.swift // Created by 刘阜澎 on 15/8/31. /* var 声明变量关键字 window 是变量名 UIWindow 变量类型 ? 可选类型在这里理解为空(nil)即可 */ //声明一个全局变量 关于swift 中变量和常量: 变量 var 声明变量关键字 var 声明没有类型,在变量的名字后面可以指定类型 如: var i:Int = 3; // 声明一个int类型的变量,变量名字为 i变量的值为 3 常量: let 常量声明关键字 let 声明没有类型,在变量的名字后面可以指定类型,常量的值是不可以改变的 let d:Double =3.1415926; d=3.5 //错误写法,因为常量的值是不可以改变的 */ 函数: swift 函数特点 (1)函数的参数中有标签(OC中的方法签名) (2)函数的返回值在函数的尾部用指针符号(箭头)指向返回值类型 (3)函数声明关键字:func */ import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,TableViewCellDelegate { //数据源 let lsitData = ["Allen","Luc","LiLei","XiaoMing"]; var _tableView:UITableView?; override func viewDidLoad() { super.viewDidLoad() self.creatUI() } func creatUI() {
let tableHeaderView:UIView=UIView(frame: CGRectMake(0,0,self.view.frame.size.width,100)) tableHeaderView.backgroundColor=UIColor.orangeColor()
_tableView=UITableView(frame:CGRectMake(0,20,300)) self.view.addSubview(_tableView!) _tableView!.delegate=self _tableView!.dataSource=self _tableView?.tableFooterView=UIView() _tableView?.tableHeaderView=tableHeaderView _tableView?.rowHeight=60 } //分组个数 func numberOfRowsInSection(section: Int) -> Int{ return 1; }
//cell 个数 func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int { print(lsitData.count); return lsitData.count; }
//创建tableViewCell func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cellIndenttifier:String = "CellIndenttifier";
let cell:TableViewCell = TableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier: cellIndenttifier); cell.delegate=self cell.textLabel?.text = lsitData[indexPath.row]; cell.textLabel?.textColor=UIColor.redColor() cell.textLabel?.font=UIFont.boldSystemFontOfSize(13.0) //圆角 cell.imageView?.image = UIImage(named:"avatars.jpg") cell.imageView?.layer.masksToBounds = true cell.imageView?.layer.cornerRadius = 5 cell.imageView?.layer.borderWidth = 2 cell.imageView?.backgroundColor=UIColor.orangeColor() cell.imageView?.layer.borderColor = UIColor.yellowColor().CGColor cell.detailTextLabel?.text = "hlello" return cell; }
//tablViewCell 点击事件 func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) { let nextView:NextViewController=NextViewController() self.presentViewController(nextView,animated: true,completion: nil) }
func buttonAction(sender:UIButton) { println("2222211") }
func a(string:NSString)->NSString { var str:NSString="string" str="string(string)" return str }
func initData() { let str:NSString=self.a("1") println(str) //初始化空字符串 var emptyString=String() if emptyString.isEmpty { println("emptySring是空字符串") }
var variableString="horse" variableString+=" and carriage" println(variableString) // horse and carriage
for Character in "Dog!" { println(Character) } // D // o // g // ! // let unusualMenagerie = "0123456" println("unusualMenagerie has (count(unusualMenagerie)) characters") } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |