swift开发小结

1.定义一个Block回调

// 1.定义一个闭包类型

typealias swiftBlock = (_ btnTag : Int) -> Void

// 2. 声明一个变量

var callBack: swiftBlock?

// 3. 定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack

func callBackBlock(block: @escaping swiftBlock) { 

 callBack = block

}

4.// 按钮点击方法

@IBAction func btnClick(_ sender: UIButton) {

//4. 调用闭包,设置你想传递的参数,调用前先判定一下,是否已实现

if callBack != nil {

   callBack!(sender.tag) 

}

5.//在ViewController的cell中

deviceCell.callBackBlock { (tag) in 

print(tag) 

 }

2.代理用法

// 1. 定协议

protocol MyDelegate { func delegateNeedDo(strMessage:String) -> ()}

// 2. 声明变量

vardelegate:MyDelegate?

// 3. 点击调用

@IBAction func btnClick(_ sender: UIButton) { delegate?.delegateNeedDo(strMessage:"\(sender.tag)")}

//4.实现代理

  headerView.delegate = self;

//代理方法

extension VC: MyDelegate {

    func delegateNeedDo(strMessage:String) {

    }

}

3.设置按钮的图片右边显示

button.semanticContentAttribute = .forceRightToLeft

4.给方法增加个block回调

   public func loginWithToken(token:String,loginSuccessBlock:(()->Void),loginErrorBlock:(()->Void)) {

        //请求成功

          loginSuccessBlock()

        //请求成功

          loginErrorBlock()  

   public func loginWithToken(token:String,loginSuccessBlock:(()->Void),loginErrorBlock:(()->Void)) {

        //请求成功

          loginSuccessBlock()

        //请求成功

          loginErrorBlock()

}

5.开辟子线程GCD方式

 DispatchQueue.global().async {

                // code

                DispatchQueue.main.async{

                    // 主线程中

                }

   }

6.snp第三方使用时有约束警告

(1) look at each constraint and try to figure out which you don't expect; 

(2) find the code that added the unwanted constraint or constraints and fix it. 

SnapKit.LayoutConstraint:0x1c4abe720@HHMainSearchHeaderCollectionReusableView.swift#41 UIView:0x10a397830.top == HKHouseHold.HHMainSearchHeaderCollectionReusableView:0x10a3757a0.top + 16.560000000000002>",

其实是由于计算得出的大小为16.560000000000002一串很长的double型与系统计算得出的高度有0.001的区别,这个时候由于两个约束的级别又是一样高的,就会产生约束警告.解决:可以将我们的约束找到报错的地方(例top)这个约束设置级别更高make.top.equalTo(RATIO*15).priorityHigh()

7.控件在表格的位置

let indexPath = self.tableView.indexPathForRow(at: textField.convert(CGPoint.zero, to:self.tableView))

获取表格某个位置的cell

let tableViewCell =self.tableView.cellForRow(at: indexPath)

8.类方法的定义

class public func 方法名

9.解决表格因为cell复杂而滑动卡顿处理

卡顿来源:由于cell界面复杂,cell展示时需要重新布局样式,这边就有大量的subviews计算布局,造成卡顿.

解决卡顿方法:将cell进行缓存处理,就第一次进入时加载cell进行缓存.

代码:

10.开发者账号注册人员已满怎么办

开发者账号注册人员已满可以用testFiglt中注册账号来增加开发人员,使用流程:1.用户和访问中添加appid账号 2.到用户邮箱中通过账号审核 3.到

底下

中添加已注册的账号,在发起邀请.4.下载app->testFight将邮件中的兑换码输入

11.cell动态更改高度防止cell会置顶

cell中:

    func updateCollectionHeight() {

        self.collectionView.snp_remakeConstraints { (make)in

            make.top.equalTo(self.grayLineView.snp_bottom).offset(RATIO *20)

            make.height.equalTo(RATIO * CGFloat(screenWidth -80) /3* CGFloat(self.imagePickerSelectArray.count /3+ (self.imagePickerSelectArray.count >=9?0:1)) + CGFloat(self.imagePickerSelectArray.count /3- (self.imagePickerSelectArray.count >=9?1:0)) *20)

            make.left.right.equalTo(0)

        }

        self.collectionView.reloadData()

        ifself.callBack!=nil{

            self.callBack!(self.imagePickerSelectArray)

        }

    }会先更新下colloection的高度,然后将事件回传到外部tableView中

controller中:

            if self.serviceCell==nil{

                self.serviceCell= (tableView.dequeueReusableCell(withIdentifier:"barCodeCellID", for: indexPath)as!HHShosesBarCodeTableViewCell)

            }

            self.serviceCell?.callBackBlock{ (array)in

                UIView.performWithoutAnimation {

                    letpath = tableView.indexPath(for:self.serviceCell!)

                    tableView.reloadRows(at: [path!], with: .bottom)

                    tableView.scrollToRow(at: path!, at: .bottom, animated:false)

                }

            }中主要是需要一个UIView.performWithoutAnimation跟scrollToRow方法来达到改变高度表格不置顶功能.还有这边self.serviceCell必须要有个变量存起来,由于在刷新高度时不存会导致cell重新初始化导致对象错乱(目前不知道是啥原因)会失效.

12.定时器timer的runLoop写法

 public var timer:Timer?

 self.remainingSeconds = 60

  weak var weakSelf =self

  self.timer=Timer.scheduleCommonModeTimer(repeatInterval:1, handler: { (timer)in

         weakSelf.remainingSeconds -= 1

    })

//timer的拓展类

extension Timer {

    publi cclass func scheduleCommonModeTimer(repeatInterval interval:TimeInterval, handler:@escaping(Timer?) ->Void) ->Timer{

        let fireDate = interval+CFAbsoluteTimeGetCurrent()

        let timer =CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, interval,0,0, handler)!

        CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, CFRunLoopMode.commonModes)

        return timer

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。