写这篇BLOG前,有些话不得不提一下,就仅当发发恼骚吧。。。

今天下午为了一个Alamofire取得数据而更新TableView的问题,查了一下午的百度(360也是见鬼的一样),竟然没有一个简单明了的回答,

而唯一几个比较接近答案的,说要 self.tableView.reloadData(),也没有贴上代码,说要放在哪个函数内,

都犹抱琵琶半遮面,让初学者自己采坑,于是郁闷了一下午,刚刚回到家,试想想,要不试试英文网,毕竟Swift就是人家老外的,

说不定老外会告诉你,怎么取得数据并绑定TableView,果不其然,用了不到3份钟的时候就完美解决。

写这篇BLOG,如果后面的新手有幸看到这篇,也可以少走很多弯路。。。

还有

特别感谢英文网

http://blog.revivalx.com/2015/02/23/uitableview-tutorial-in-swift-using-alamofire-haneke-and-swiftyjson/

有一点特别注意的是,方法 self.tableView.reloadData() 要在变量wifi改变的时候立马加入

直接看以下的代码了。。。

//
// TableViewController.swift
// MyTableView-1387
//
// Created by apiapia on 17/1/6.
// Copyright © 2017年 apiapia. All rights reserved.
//$ curl http://httpbin.org/get
//
//{
// "args": {},
// "headers": {
// "Accept": "*/*",
// "Connection": "close",
// "Content-Length": "",
// "Content-Type": "",
// "Host": "httpbin.org",
// "User-Agent": "curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3"
// },
// "origin": "24.127.96.129",
// "url": "http://httpbin.org/get"
//} // import UIKit
import Alamofire
import SwiftyJSON class TableViewController: UITableViewController { var wifi = ["StarBuck","MJ"]
// var wifi = [String]()
let url = "https://httpbin.org/get"
//上面wifi可以用 Alamofire获取远程数据,
//http://httpbin.org/get override func viewDidLoad() {
super.viewDidLoad() // Alamofire取得的网络数据是异步的
Alamofire.request(url, method: .get).validate().responseJSON { (response) in
// print (response.request)
switch response.result.isSuccess {
case true: if let value = response.result.value {
let json = JSON(value) let headers:Dictionary<String,Any>=json["headers"].dictionaryValue print (headers)
// populating tableview with json from url using Alamofire
// http://blog.revivalx.com/2015/02/23/uitableview-tutorial-in-swift-using-alamofire-haneke-and-swiftyjson/
// self.wifi = ["1","2","3","4"]
// self.tableView.reloadData() self.wifi = [String]() // 重新生成数组成功
// ["Accept-Language", "Host", "Accept", "User-Agent", "Accept-Encoding"]
for (index,_) in headers{ print (index)
self.wifi.append(index) } self.tableView.reloadData() } case false:
print ("网络请求失败") }
} print ("wifi现在是:",wifi) // Alamofire.request(url).validate().responseJSON { response in
// switch response.result.isSuccess {
// case true:
// if let value = response.result.value {
// let json = JSON(value)
// if let number = json[0]["phones"][0]["number"].string {
// // 找到电话号码
// print("第一个联系人的第一个电话号码:",number)
// }
// }
// case false:
// print("")
// }
// }
// //注册表格
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "wifiCell")
//去掉表格尾部空行
self.tableView.tableFooterView = UIView(frame: CGRect.zero) self.tableView.reloadData() } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 3 //共有三个分区
} //返回三个分区相应的表格行数
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if section == 1 { return self.wifi.count }
else{
return 1 } } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "wifiCell", for: indexPath)
cell.textLabel?.text = self.wifi[indexPath.row] return cell
}else{
return super.tableView(tableView, cellForRowAt: indexPath)
} } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.section == 1 { return 50 }else {
return super.tableView(tableView, heightForRowAt: indexPath)
}
} //取消高亮直选
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true)
} // 当 override了一个静态表格的时候要用到 indentationLevelForRowAt这个方法
// 因为 数据源 对新加进来的 cell一无所知 ,所以要用这个代理方法
override func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int { if indexPath.section == 1 { let newIndexPath = IndexPath(row: 0, section: indexPath.section)
return super.tableView(tableView, indentationLevelForRowAt: newIndexPath) }else { return super.tableView(tableView, indentationLevelForRowAt: indexPath)
}
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} /*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/ /*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/ /*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { }
*/ /*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/ /*
// MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/ }

【Swift】Alamofile网络请求数据更新TableView的坑的更多相关文章

  1. ios MVVM实践 刷新网络请求+tableView展示数据

    [实现效果] [目录结构相关] 此示例展示用的是MVVM结构形式,表述如下 M:数据Model的存储,可以用来对属性进行处理.(即胖model概念,上图中xx万人订阅这个处理方法写在Model内) V ...

  2. iOS开发——实战篇Swift篇&UItableView结合网络请求,多线程,数据解析,MVC实战

    UItableView结合网络请求,多线程,数据解析,MVC实战 学了这么久的swift都没有做过什么东西,今天就以自己的一个小小的联系,讲一下,怎么使用swift在实战中应用MVC,并且结合后面的高 ...

  3. Swift基础之Demo包含刷新,加载,网络请求,MVC

    Swift中有一个Alamofire第三方是进行网络请求的,它是AFNetworking的作者写的Swift形式,今天先介绍一下,利用pod导入AFNetworking,SVProgressHUD,M ...

  4. iOS开发--Swift 基于AFNetworking 3.0的网络请求封装

    Swift和OC基于AFNetworking的网络请求流程相同, 就是语法不同, 对于Swift语法不是很清楚的同学, 建议多看看API文档, 自己多多尝试. 写过OC的应该都明白每句话做什么的, 就 ...

  5. swift中第三方网络请求库Alamofire的安装与使用

    swift中第三方网络请求库Alamofire的安装与使用 Alamofire是swift中一个比较流行的网络请求库:https://github.com/Alamofire/Alamofire.下面 ...

  6. Swift使用Alamofire实现网络请求

    Alamofire是一个用Swift编写的HTTP网络库,由此前热门开源项目AFNetworking的的作者mattt开发,可非常简单地用于异步网络通信. 要获取最新版本的 Alamofire,前往h ...

  7. 微信小程序 网络请求之re.request 和那些坑

    微信小程序有四种网络请求类型,下面只详细介绍普通HTTPS请求(wx.request) 普通HTTPS请求(wx.request) 上传文件(wx.uploadFile) 下载文件(wx.downlo ...

  8. Swift基础之使用Alamofire库进行网络请求和断点下载

    好久没有写过Swift相关的文章博客了,这里我就展示一下关于使用Alamofire库的方法 1.什么是Alamofire (1)Alamofire 的前身是 AFNetworking.AFNetwor ...

  9. post网络请求坑

    微信小程序开发中网络请求必不可少.GET.POST请求是最常用的.GET请求 POST请求的时候有好几个坑.我已经为大家填好了.

随机推荐

  1. 阿里云本地FTP怎么连接?通用win7,win8,win8.1,win10

  2. for循环与for in,$('').each 与$.each的区别

    一:for循环与for in的区别 for...in 语句用于对数组或者对象的属性进行循环操作. 语法: for (变量 in 对象){    在此执行代码} for循环是对数组的元素进行循环,而不能 ...

  3. Coding4Fun Toolkit支持本地化解决办法

    在项目中需要使用Coding4Fun Toolkit中的TimePicker控件, 1. 但是在中文系统下显示的却是英文: 2. 最后发现,需要在源代码中添加中文资源,并重新编译出包含中文语言的dll ...

  4. 前端开发css实战:使用css制作网页中的多级菜单

    前端开发css实战:使用css制作网页中的多级菜单 在日常工作中,大家都会遇到一些显示隐藏类菜单,比如页头导航.二维码显示隐藏.文本提示等等......而这些效果都是可以使用纯css实现的(而且非常简 ...

  5. C# - 网络编程 之 TcpClient与TcpListener

    TcpClient类 TcpListener类 TCP通信 UDP通信 参考:

  6. CentOS6.8 修改主机名(1)

    1.临时修改主机名   显示主机名:spark@master:~$ hostnamemaster修改主机名:spark@master:~$ sudo hostname hadoopspark@mast ...

  7. 使用insertBefore实现insertAdjacentHTML()

    Element.insertAdjacentHTML()方法由IE引入,并在HTML5中标准化,它将任意的HTML标记字符串插入到指定的元素“相邻”的位置. insertAdjacentHTML()有 ...

  8. 面向对象设计模式纵横谈:Singelton单件模式(笔记记录)

       李建忠老师讲的<面向对象设计模式纵横谈>,早就看过了,现在有了时间重新整理一下,以前的博客[赛迪网]没有了,现在搬到博客园,重新过一遍,也便于以后浏览. 设计模式从不同的角度分类会得 ...

  9. eclipse SE增加Web开发插件

    最近接触了些java项目,之前安装了eclipse SE版本.没有Web开发插件,调试不了Web代码.点击“Window”--“Preference” 左边菜单栏是找不到“Server”项来配置服务器 ...

  10. dirname 命令

    dirname 命令用途将指定路径除了最后以外的部分写到标准输出.语法dirname Path描述dirname 命令读取指定路径名保留最后一个"/"(斜杠)及其后面的字符,删除其 ...