Swift学习总结(个人总结,以方便理解为主)

本文总结了Swift编程中的关键点,包括注册UITableViewCell的方法、UIViewController与UITableViewDataSource的关系、懒加载实现、宏定义、Swift中循环布局的挑战、协议与Block的使用、协议遵守时的错误处理、JSON数据解析以及Swift与Objective-C的混合使用等,旨在帮助开发者更好地理解和应用Swift。
摘要由CSDN通过智能技术生成

swift总结

一、注册cell

1.用nib, 向 tableview 注册 cell

全局变量 let cellIdentifier = "myCell"

myTableView!.registerNib(UINib(nibName: "MyCell", bundle:nil), forCellReuseIdentifier: cellIdentifier)

然后在 cellForRowAtIndexPath 方法中使用:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {                   

        var mycell : MyCell = tableView!.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as MyCell

        return mycell

    }

2.用纯代码,向tableView注册cell

全局变量 let cellIdentifier = "myCell"

self.tableView.registerClass(MyCell.classForKeyedArchiver(), forCellReuseIdentifier: cellIdentifier)

然后在 cellForRowAtIndexPath 方法中使用

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyCell

        return cell

    }

3.不注册cell,系统直接创建

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!

{

    let identifier = "CellIdentifier"

    var cell :UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(identifier) as? MyCell

    cell = MyCell(style: .Default, reuseIdentifier: identifier)

    var string :String = typeArray[indexPath.row] as String

    cell!.textLabel.text = string

    return cell

}

二、UIViewController 无法服从 UITableViewDataSource 所以最好用UITableViewController

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值