1、根据返回的json建立model
比如首页的列表需要用到的,新建一个Swift File
PHP Code复制内容到剪贴板
struct IndexListCell {
varavatarImg =""// 头像
varnickname =""// 昵称
varposition =""// 地点
varlikeNum = 0// 点赞的人
}
命名为:IndexListCell
这里注明了首页需要用到的字段,在控制器给它配置值,并渲染列表数据
2、新建tableviewcontroller,storyboard关联,打开新建好的TableViewController:
PHP Code复制内容到剪贴板
// IndexListCell是在model中根据json内容提取出来需要的字段
import Just
import Kingfisher
let getIndexUrl = "http://xxx.com/api/v1/app-test/list";
varIndexList : [IndexListCell] = []
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
loadListData()
// 下拉刷新
self.refreshControl = UIRefreshControl()
// 刷新的动作是loadListData
refreshControl?.addTarget(self, action: #selector(loadListData), for: .valueChanged)
}
@objc func loadListData() {
Just.post(getIndexUrl) { (res) in
guard let json = res.json as? NSDictionaryelse{
// 保证结果为json 并且解析为字典,否则就return
return
}
let listData = TravelList(fromDictionary: json).data! // 强制转换
self.IndexList = listData.map({ (data) -> IndexListCell in
returnIndexListCell(avatarImg: data.avatar, nickname: data.nickname, position: data.positon, likeNum: data.like)
})
// 取到数据以后 ,更新主线程代码
OperationQueue.main.addOperation {
self.tableView.reloadData()
// 刷新过以后,刷新的动作要停止掉
self.refreshControl?.endRefreshing()
}
}
}
// 几个区块
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return1
}
// 返回列表总数
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
returnIndexList.count
}
// 渲染列表内容
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// indexCell是在storyboard中选择cell添加的ID,首先,取得单个的ID,强制转换为IndexTableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "indexCell",for: indexPath)as! IndexTableViewCell
// 取单行的数据,绑定变量
let list = IndexList[indexPath.row]
cell.IndexNickname.text = list.nickname
cell.IndexPosition.text = list.position
cell.IndexLikeNum.text = "\(list.likeNum)"
// 头像 用到kingfisher第三方组件
let imgUrl = URL(string: list.avatarImg)
cell.IndexAvatar.kf.setImage(with: imgUrl)
// 大图
cell.IndexBigPic.kf.setImage(with: imgUrl)
returncell
}
3、还是在当前控制器,先取到下一个指定的控制器,在下一个控制器中指定接收的变量,然后在当前控制器将参数传递进去
PHP Code复制内容到剪贴板
override func prepare(forsegue: UIStoryboardSegue, sender: Any?) {
// 隐藏自带的导航
// navigationController?.setNavigationBarHidden(true, animated: true)
// 取到下个控制器, 下个界面的控制器就是IndexViewController,并把值传递给下一个界面的
let dest = segue.destination as! IndexViewController
dest.detail = IndexList[(tableView.indexPathForSelectedRow?.row)!]
}
在IndexViewController中,定义一个detail变量名,来接收值;
IndexViewController:
PHP Code复制内容到剪贴板
import Kingfisher
vardetail : IndexListCell!// 加感叹号表示它一定是有值的
// 定义几个需要显示的控件
@IBOutlet weak varNickname: UILabel!
@IBOutlet weak varAvatarImg: UIImageView!
@IBOutlet weak varbackgroundImg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// dump(detail)
// 将除了背景以外的元素置顶
bringBtnToFront()
// 设置数据 并且背景虚化 + 背景图片
setData()
}
// 将按钮置于上层
func bringBtnToFront() {
view.bringSubview(toFront: Nickname)
view.bringSubview(toFront: AvatarImg)
}
func setData() {
// 设置昵称
Nickname.text = detail.nickname
let imgUrl = URL(string: detail.avatarImg)
AvatarImg.kf.setImage(with: imgUrl)
backgroundImg.kf.setImage(with: imgUrl)
// 使用UIBlurEffect来作背景虚化
let blurEffect = UIBlurEffect(style: .light)
let effectView = UIVisualEffectView(effect: blurEffect)
// 虚化的容器大小 第一种:等于背景图片的大小
// effectView.frame = ImgBackground.bounds
// 虚化的容器大小,第二种:等于当前视图的大小,即全屏显示
effectView.frame = self.view.bounds
// 将它加入到背景中
backgroundImg.addSubview(effectView)
}
如果添加背景层,将它拉到最上面。
列表页:
详情页: