Swift.微博

屏幕快照 2018-03-30 下午4.02.45.png

1,首页cell用到了存储高度布局

初始一个cell用来做布局高度计算

      self.layoutCell = HomeCell.init(style: UITableViewCellStyle(rawValue: 0)!, reuseIdentifier: "HomeCell")
//cell高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model:WeiBoModel = self.cellArr[indexPath.row] as! WeiBoModel
        if  model.cellHeight == 0{
        model.cellHeight = self.layoutCell.heightForModel(weiboModel: model)
        }
        return model.cellHeight
 
    }

在自定义cell 中

    //用来计算布局 给cell高度 
    func heightForModel(weiboModel : WeiBoModel) -> CGFloat {
        self.weiboModel = weiboModel
        self.layoutIfNeeded()
        return self.cellSegmentationView.frame.maxY
    }

2,文件读取 存入

//文件读取 存入

/// 根据json文件名写入json
    ///
    /// - Parameters:
    ///   - jsonObject: jsonobjec(字典数组对象)
    ///   - name: json文件名
    /// - Returns: 是否写入成功
   static func writeJson(jsonObject : AnyObject , name : NSString) -> Bool {
        var  sp = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .allDomainsMask, true)
        let jsonName = NSString.init(format: "%@/%@.json", sp[0], name)
        let jsonData:NSData = try! JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted) as NSData
        return  jsonData.write(toFile: jsonName as String, atomically: true)
    }
    
    
    
    /// 根据json文件名 读取json
    ///
    /// - Parameter name: json名
    /// - Returns: jsonobjec(字典数组对象)
   static func readJson(name : NSString) -> AnyObject {
        var  sp = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .allDomainsMask, true)
        let jsonName = NSString.init(format: "%@/%@.json", sp[0], name)
        let data:NSData
        do {
            data =   try NSData.init(contentsOfFile: jsonName as String)
        } catch let err as NSError {
            print("err:\(err.description)")
            return NSNull()


        }
    
        guard  let jsonObject = try?JSONSerialization.jsonObject(with: data as Data, options: .mutableContainers) else {
            return NSNull()
        }
        return jsonObject as AnyObject
    }
    

另:swift 一些注意事项
https://www.jianshu.com/p/93da48b42e78

https://www.jianshu.com/p/ebc76153b1ec

demo:https://github.com/MuRanJiangXia/SwiftWeibo

欢迎大家给个star鼓励一下!!

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

推荐阅读更多精彩内容