1/ 创建一个 可变数组
var dataList: NSMutableArray = ["qqqqq","wwwwwwww","eeeeeee","rrrrrrrrrr","tttttttt","yyyyyyyyyy","uuuuuuuuuuuu","iiiiiiiiiiii","oooooooooooo","ppppppppppp","aaaaaaaaaa","ssssssssss","ddddddddddd","ffffffffff","ggggggggggggg","hhhhhhhhhhhhh"];
2/ 闭包 相当于 OC 中的 block
var zyTableView: UITableView = {
var tempTableView = UITableView (frame: self.view.bounds)
tempTableView.delegate = self
tempTableView.dataSource = self
return tempTableView
}()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let identifier: String = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")
if !(cell != nil) {
var cell = UITableViewCell (style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
}
cell.textLabel?.text = self.dataList[indexPath.row] as? String
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == UITableViewCellEditingStyle.Delete){
dataList.removeObjectAtIndex(indexPath.row)
tableView .deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Middle)
}else if(editingStyle == UITableViewCellEditingStyle.Insert){
dataList .insertObject("章鱼哥", atIndex: indexPath.row+1)
let zyIndexPath = NSIndexPath (forRow: indexPath.row+1, inSection: indexPath.section)
zyTableView .insertRowsAtIndexPaths([zyIndexPath], withRowAnimation: UITableViewRowAnimation.Middle)
}
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if(indexPath.row % 2 == 1){
return UITableViewCellEditingStyle.Insert
}
else{
return UITableViewCellEditingStyle.Delete
}
}
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let source: AnyObject = self.dataList[sourceIndexPath.row]
dataList .removeObjectAtIndex(sourceIndexPath.row)
dataList .insertObject(source, atIndex: destinationIndexPath.row)
}
var zyScrollView: UIScrollView = {
var tempScrollView: UIScrollView = UIScrollView()
tempScrollView.frame = self.view.frame
tempScrollView.delegate = self
//设置边距
tempScrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20)
//不显示水平滚动标示
tempScrollView.showsHorizontalScrollIndicator = false
//不显示垂直滚动标示
tempScrollView.showsVerticalScrollIndicator = false
//设置偏移
// *** 偏移位置
tempScrollView.contentOffset = CGPointMake(0, -100)
// 取消弹簧效果,内容固定,不希望出现弹簧效果时
// 不要跟bounds属性搞混了
tempScrollView.bounces = false
// 设置最大/最小缩放比例
tempScrollView.minimumZoomScale = CGFloat(0.2)
tempScrollView.maximumZoomScale = CGFloat(2.0)
tempScrollView.backgroundColor = UIColor .cyanColor()
return tempScrollView
}()
5/
// 让图像视图根据图像自动调整大小
tempImage .sizeToFit()
/**
1> 设置了代理
2> 指定了最大、最小的缩放比例
表示ScrollView是可以缩放的
代理方法的"返回值"实际上就是控制器告诉滚动视图,要缩放的是UIImageView
*/
// 告诉ScrollView要缩放的视图是谁,具体的缩放实现,是由ScrollView来完成的
// 1> scrollView要知道缩放谁
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.zyImageView;
}
// 2> 滚动视图即将开始缩放,通常不需要写
func scrollViewWillBeginZooming(scrollView: UIScrollView, withView view: UIView?) {
print(__FUNCTION__)
}
// 3> 正在缩放,通常也不需要实现
func scrollViewDidZoom(scrollView: UIScrollView) {
print(__FUNCTION__)
print(NSStringFromCGAffineTransform(zyImageView.transform))
}
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
print(__FUNCTION__)
}
var messageFrames: NSMutableArray = {
let array: NSArray = NSArray (contentsOfFile: NSBundle .mainBundle() .pathForResource("messages.plist", ofType: nil)! )!
var arrayM: NSMutableArray = NSMutableArray()
var dictionary: NSDictionary = NSDictionary()
for dictionary in array{
var message: ZYMessage = ZYMessage(dictionary: dictionary as! NSDictionary)
let lastFm: ZYMessageFrame? = arrayM.lastObject as? ZYMessageFrame
let string: NSString = message.time
let messageFrame: ZYMessageFrame = ZYMessageFrame()
messageFrame.message = message
arrayM.addObject(messageFrame)
}
return arrayM
}()
(2)XXFrames 数组存的事 XXFrame元素 (含有 XX 的模型数据)class ZYMessage: NSObject {
override init(){
super.init()
}
// 添加的属性 需要在cell中 展示的数据
var text:NSString = ""
var time:NSString = ""
init(dictionary: NSDictionary) {
super.init()
text = dictionary .objectForKey("text") as! NSString
time = dictionary .objectForKey("time") as! NSString
}
class func messageWithDict(dictionary: NSDictionary) -> ZYMessage{
return ZYMessage(dictionary: dictionary)
}
}
class ZYMessageFrame: NSObject {
// 分别定义cell中 各控件的 frame
let padding: CGFloat = 10
let textPadding: CGFloat = 20
var textF: CGRect = CGRectZero
var timeF: CGRect = CGRectZero
var iconF: CGRect = CGRectZero
var cellHeight: CGFloat = 0
let maxF = CGFloat(MAXFLOAT)
var message: ZYMessage? = nil{
didSet{
timeF = CGRectMake(timeX, timeY, timeW, timeH)
iconF = CGRectMake(iconX, iconY, iconW, iconH)
//根据文字长度求 宽高
let textSize = message!.text.boundingRectWithSize(CGSizeMake(150, maxF), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont .systemFontOfSize(15)], context: nil).size
//4. cell的高度
let iconMaxY = CGRectGetMaxY(iconF)
let textMaxY = CGRectGetMaxY(textF)
cellHeight = max(iconMaxY, textMaxY)
}
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let messageFrame: ZYMessageFrame = self.messageFrames[indexPath.row] as! ZYMessageFrame
return messageFrame.cellHeight
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:ZYTalkTableViewCell? = tableView.dequeueReusableCellWithIdentifier(ID) as? ZYTalkTableViewCell
if (cell == nil) {
cell = ZYTalkTableViewCell (style: UITableViewCellStyle.Default, reuseIdentifier: ID)
}
cell?.messageFrame = messageFrames[indexPath.row] as! ZYMessageFrame
return cell!
}
(7)自定义cellclass ZYTalkTableViewCell: UITableViewCell {
let textPadding: CGFloat = 20
var timeLabel: UILabel = UILabel()
var icomImageView: UIImageView = UIImageView ()
var button: UIButton = UIButton ()
var messageFrame: ZYMessageFrame = ZYMessageFrame(){
didSet{
let message: ZYMessage = messageFrame.message!
//1. 时间
timeLabel.frame = messageFrame.timeF
timeLabel.text = message.time as String
icomImageView.frame = messageFrame.iconF
//3. 正文
button .setTitle(message.text as String, forState: UIControlState.Normal)
button.frame = messageFrame.textF
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code 加载XIB后的初始化代码
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure theb view for the selected state
}
按照这个样式来写 初始化方法
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//1. 时间
timeLabel.textAlignment = NSTextAlignment.Center
timeLabel.font = UIFont .systemFontOfSize(13.0)
self.contentView .addSubview(timeLabel)
//2. 头像
self.contentView .addSubview(icomImageView)
//3. 正文
button.titleLabel?.font = UIFont .systemFontOfSize(15.0)
button.titleLabel?.numberOfLines = 0
button.setTitleColor(UIColor .blackColor(), forState: UIControlState.Normal)
button.contentEdgeInsets = UIEdgeInsetsMake(textPadding, textPadding, textPadding, textPadding)
self.contentView .addSubview(button)
self.backgroundColor = UIColor .clearColor()
}
这个函数 必须加
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func resizeImageWithName(name: String) -> UIImage{
let image: UIImage = UIImage (named : name)!
let w = image.size.width * 0.5 - 1
let h = image.size.height * 0.5 - 1
return image .resizableImageWithCapInsets(UIEdgeInsetsMake(h, w, h, w))
}
}