微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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("1111111")

}

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")

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐