swift总结2

来源:互联网 发布:爰淘宝商城 编辑:程序博客网 时间:2024/06/03 14:47

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

}()


3/ cell 的定义  及 cell 左滑  cell 移动 

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)

        }elseif(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){

            returnUITableViewCellEditingStyle.Insert

        }

       else{

            returnUITableViewCellEditingStyle.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)

    }


4/  UIScrollview 

 var zyScrollView:UIScrollView = {

       var tempScrollView: UIScrollView = UIScrollView()

        tempScrollView.frameself.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()


6/  UIScrollview 代理方法   缩放 

    /**

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

    }

    

7/  可变高度的cell  需要用代码创建 
(1) 在tableview的控制器中 添加 XXFrames的可变数组

    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.lastObjectas? ZYMessageFrame

           let string: NSString = message.time

           let messageFrame: ZYMessageFrame = ZYMessageFrame()

            messageFrame.message = message

            arrayM.addObject(messageFrame)

        }

       return arrayM

    }()

(2)XXFrames 数组存的事 XXFrame元素 (含有 XX 的模型数据)
(3)定义XX 模型数据 

class ZYMessage:NSObject {

    overrideinit(){ 

       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)

    }

}

(4)定义XXFrame 类 

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)

        }

    }


}

(5)在控制器中 返回的cell高度 

func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath) -> CGFloat {

       let messageFrame: ZYMessageFrame = self.messageFrames[indexPath.row]as! ZYMessageFrame

       return messageFrame.cellHeight

    }

(6)在cell中传入的数据 也是 XXFrame 而不是 XX数据模型 

    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)自定义cell 

class 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.timeas String

            icomImageView.frame =messageFrame.iconF

      

           //3. 正文

           button .setTitle(message.textas 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))      

    }

}







0 0
原创粉丝点击