美文网首页
Swift 请求数据与表格

Swift 请求数据与表格

作者: 鳄佛 | 来源:发表于2018-08-24 19:19 被阅读0次

    1.创建一个类NSurlSevice

    创建一个方法

    func getNew(channel:String,starNum:Int,complation:@escaping (Any,Bool) -> Void) -> Void {

            //网址

            var strUrl = "http://api.jisuapi.com/news/get?channel=\(channel)&start=\(starNum)&num=10&appkey=de394933e1a3e2db"

            //转码

            strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed)!

            //转化为url类型

            let url = URL(string: strUrl)

            //把url封装成nsurlquest

            let request = URLRequest(url: url!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 15.0)

            //用URLsession请求网络数据

            let task:URLSessionDataTask = URLSession.shared.dataTask(with: request) { (data, resposen, error) in

                //错误判断

                if error != nil{

                    complation("网络服务器错误", false)

                    return

                }

                //解析

                let  jsonData = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)

                if jsonData == nil{

                    complation("网络解析错误", false)

                    return

                }

                //解析成功

                let status = (jsonData as! NSDictionary).value(forKey: "status") as! String

                let msg = (jsonData as! NSDictionary).value(forKey: "msg") as! String

                if Int(status) != 0 {

                    complation(msg, false)

                    return

                }

                let result = (jsonData as! NSDictionary).value(forKey: "result") as! NSDictionary

                let list = result.value(forKey: "list") as! NSArray

                var newarr:[News] = []

                for item in list{

                    let dic = item as! NSDictionary

                    let oneNew = News()

                    oneNew.title =  dic.value(forKey: "title") as! String

                    oneNew.content = dic.value(forKey: "content") as! String

                    oneNew.time = dic.value(forKey: "time") as! String

                    print("解析1:\(oneNew.title)")

                    print("解析2:\(oneNew.content)")

                    print("解析3:\(oneNew.time)")

                    newarr.append(oneNew)

                }

                complation(newarr, true)

            }

          task.resume()

        }

    图一

    2.创建一个继承与UITableViewCell

    var img: UIImageView?

        var Zlable:UILabel?

        var Flabel:UILabel?

        var Time:UILabel?

        func showint() -> Void {

            img = UIImageView(frame: CGRect(x: 5, y: 5, width: 90, height: 90))

            img?.backgroundColor = .red

            self.contentView.addSubview(img!)

            Zlable = UILabel(frame: CGRect(x: 110, y: 20, width: 170, height: 40))

            Zlable?.font = UIFont(name: "ni", size: 29)

            Zlable?.backgroundColor = UIColor.blue

            contentView.addSubview(Zlable!)

            Flabel = UILabel(frame: CGRect(x: 110, y: 65, width: 70, height: 30))

            Flabel?.backgroundColor = UIColor.orange

            contentView.addSubview(Flabel!)

            Time = UILabel(frame: CGRect(x: 280, y: 30, width: 80, height: 20))

            Time?.backgroundColor = UIColor.yellow

            contentView.addSubview(Time!)

        }

        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

            super.init(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier)

            self.showint()

        }

        required init?(coder aDecoder: NSCoder) {

            fatalError("init(coder:) has not been implemented")

        }

    cell

    3.在viewcontroller中调用

    图3

    图3中的方法在viewWillAppear中调用

    4.在tableview中注册注册cell

            TableView.register(TableViewCell.classForCoder(), forCellReuseIdentifier: "xuwen")

    相关文章

      网友评论

          本文标题:Swift 请求数据与表格

          本文链接:https://www.haomeiwen.com/subject/vkqzyftx.html