在 CollectionView 上重新加载数据,Swift
reloadData on CollectionView, Swift
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! UsersCollectionViewCell
someRequest(username: self.usernameUrl) { (userInfo, error) in
guard let userInfo = userInfo else {
// present error
return
}
print("Running")
let user_image_url = userInfo.items.map{([=11=].avatarURL)}
cell.userCellLabel.text = user_name[indexPath.item]
}
return cell
}
为什么当我在 viewDidAppear 上调用 reloadData 时,someRequest( ... ) 中的代码不是 运行?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.collectionViewUsers.reloadData()
}
P.s。 Somerequest 只是执行一个 Alamofire get
异步 Alamofire 请求必须在 cellForItemAt
处理程序之外。因为每次单元格出现在屏幕上时都会调用 cellForItemAt
,这意味着它会在滚动时多次尝试为同一单元格获取相同的数据,这不是预期的。所以 viewDidLoad
是开始抓取的好地方,异步调用完成后不要忘记重新加载 collectionview。
var user_image_url: String?
override func viewDidLoad(_ animated: Bool) {
someRequest(username: self.usernameUrl) { (userInfo, error) in
guard let userInfo = userInfo else {
// present error
return
}
print("Running")
user_image_url = userInfo.items.map{([=10=].avatarURL)}
self.collectionViewUsers.reloadData()
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! UsersCollectionViewCell
if let user_image_url = user_image_url {
//user_image_url is avaliable
}
return cell
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! UsersCollectionViewCell
someRequest(username: self.usernameUrl) { (userInfo, error) in
guard let userInfo = userInfo else {
// present error
return
}
print("Running")
let user_image_url = userInfo.items.map{([=11=].avatarURL)}
cell.userCellLabel.text = user_name[indexPath.item]
}
return cell
}
为什么当我在 viewDidAppear 上调用 reloadData 时,someRequest( ... ) 中的代码不是 运行?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.collectionViewUsers.reloadData()
}
P.s。 Somerequest 只是执行一个 Alamofire get
异步 Alamofire 请求必须在 cellForItemAt
处理程序之外。因为每次单元格出现在屏幕上时都会调用 cellForItemAt
,这意味着它会在滚动时多次尝试为同一单元格获取相同的数据,这不是预期的。所以 viewDidLoad
是开始抓取的好地方,异步调用完成后不要忘记重新加载 collectionview。
var user_image_url: String?
override func viewDidLoad(_ animated: Bool) {
someRequest(username: self.usernameUrl) { (userInfo, error) in
guard let userInfo = userInfo else {
// present error
return
}
print("Running")
user_image_url = userInfo.items.map{([=10=].avatarURL)}
self.collectionViewUsers.reloadData()
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! UsersCollectionViewCell
if let user_image_url = user_image_url {
//user_image_url is avaliable
}
return cell
}