- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试动态更新 uicollectionview。我用了this amazing tutorial关于如何创建简单的 uicollection。
它在使用静态项目数组时效果很好。我的问题 - 我想让 uicollection 填充我从数据库解析到新数组中的数据。我不确定在解析我的 json 数据后如何重新加载 uicollection。
带有答案的更新代码:
import UIKit
class Books: UIViewController, UICollectionViewDelegate {
@IBOutlet weak var bookscollection: UICollectionView!
var user_id: Int = 0;
//------------ init ---------------//
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
showtutorial()
getuserid()
}
//------------ show books ---------------//
var booknames = [String]()
var bookcolor = [String]()
var bookdescription = [String]()
var bookid = [Int]()
func posttoapi(){
//show loading
LoadingOverlay.shared.showOverlay(view: self.view)
//send
let url:URL = URL(string: "http://www.url.com")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let paramString = "user_id=\(user_id)"
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest) {(data, response, error) in
//hide loading
LoadingOverlay.shared.hideOverlayView()
//no response
guard let data = data, let _:URLResponse = response, error == nil else {
print("response error")
return
}
//response, parse and send to build
let json = String(data: data, encoding: String.Encoding.utf8);
if let data = json?.data(using: String.Encoding.utf8){
let json = try! JSON(data: data)
for item in json["rows"].arrayValue {
//push data to arrays
self.booknames.append(item["name"].stringValue)
self.bookcolor.append(item["color"].stringValue)
self.bookdescription.append(item["description"].stringValue)
self.bookid.append(item["id"].int!)
//reload uicollection
DispatchQueue.main.sync(execute: {
self. bookscollection.reloadData()
})
}
}
}
task.resume()
}
//------------ collection -------------//
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("collection view code called")
return self.booknames.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = bookscollection.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! BooksCell
cell.myLabel.text = self.booknames[indexPath.item]
cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.item)!")
}
//------------ end ---------------//
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
感谢您的帮助!
最佳答案
我遇到了同样的问题..你的 url 有很多图片意味着重新加载 t
dispatch_async(dispatch_get_main_queue(), {
self.collectionView.reloadData()
})
关于ios - 数据解析后重新加载 UICollection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44261236/
我有这门课,我将在顶部菜单 View 上工作。 这是代码: import UIKit class TopHomeMenuBar: UIView { let collectionView: UIColl
我正在尝试创建一个包含两列的表格,其中偶数行比奇数行高。这就是我目前拥有的。 import UIKit class FBSCollectionViewCell: UICollectionViewCel
我创建了一个具有自定义布局的 UICollection,以允许垂直和水平滚动。它是一个由相等的部分和每个部分中的项目组成的网格(即 10 x 10、20 x 20 等)。我希望能够放置两个保留在 Vi
Collection View 单元中有一个按钮,当我单击该按钮时,目标是转到更详细的 View Controller 。 class WelcomeViewController: UIViewCo
我正在尝试制作可以过滤 UICollectionView 搜索结果的搜索栏。我制作了一个自定义 UICollectionViewCell 类 class toolCollectionViewCell:
我制作了一个 UICollection View 。在我的 ViewController 中,我有以下代码: import UIKit import AlamofireImage class MYVi
你好,我正在使用 UICollectionView 在我的 iPhone 应用程序中显示图像,但是当我 ScrollView 时,加载的图像消失并再次加载图像,这是因为“dequeueReusable
我正在使用 UICollectionView 来存储图像,我可以通过覆盖 CanMove 和 MoveItem 来重新排序它们。 但 UICollection 中的项目只有在单元格尺寸较大时才会重新排
我创建了一个自定义 UICollection 单元格,但数据有时会出现在错误的单元格中,无论我刷新多少 UICollection View ,它都拒绝更改。我已经打印出来,看看从数组中获取的数据是否有
我在我的应用程序中添加了一个 UICollectionView。我已经给出了它的约束,例如 Collection View 的前导、尾随、顶部和一些固定高度。它正在显示所有项目。但我无法滚动它。请问是
我正在尝试动态更新 uicollectionview。我用了this amazing tutorial关于如何创建简单的 uicollection。 它在使用静态项目数组时效果很好。我的问题 - 我想
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我制作了一个仅包含 2 个单元格的 UICollection,其页脚包含一个按钮“显示更多”。当我们点击按钮“显示更多”时,它应该在 UICollection 中追加 2 个单元格。 我的问题是,当我
默认情况下,当您在 Collection View 中使用流布局时,单元格垂直居中。有没有办法改变这种对齐方式? 最佳答案 采用面向功能的方法的 Swift 4: class TopAlignedCo
目前我有一个 collection view,它占据了 3.5 英寸屏幕尺寸的大部分屏幕。collection view 距离屏幕顶部 51 像素,距离屏幕底部 20 像素。我将单元格与 UIPage
我遇到以下问题: 我有一个带有 UICollection View 的 View Controller ,文本数据(名称、图像的 url)来自使用 swiftyJSON 的 json,但所有图像都是以
每次打开具有 Collection View 的 View Controller 时,我都会收到以下消息 [ setValue:forUndefinedKey:]: this class is not
我需要在导航回该特定 View 时重新加载 Collection View 。 我尝试实现以下所有内容,但它不起作用 1.viewDidAppear()中调用self.collectionView.r
我试图让 UICollectionView 快速回到靠近它的任何项目单元格的中心,但问题是,当我稍微移动它时,它会以太快的速度弹回单元格的中心,而单元格的中心看起来并不光滑,是否可以忽略或禁用滚动,除
我在使用 UICollectionViews 时遇到了 2 个问题,我以某种方式相信它在这两种情况下都与 IndexPath 相关,但是我真的不知道 Indexpath 何时被调用或它究竟做了什么以及
我是一名优秀的程序员,十分优秀!