我正在尝试从服务器获取数据,并且我正在加载到collectionView
。在这里,我在一个collectionView
中使用了3个ViewController
,我使用xib
作为collectionView
cell
。我对那些3 cell
使用相同的collectionView
。但我无法将数据重新加载到collectionView
。
这是我的代码。
//MARK:- Service Cell
func SetUpAlterationUIdata()
{
let headers = ["Authorization":"Token token=1aab17fe5281e49975a530db411f7de4"] as [String : String]
self.showSpinner("Loading...", "Please wait!!")
DispatchQueue.global(qos: .background).async {
AFWrapper.AFsharedInstance.requestGETURL(Get_Hod_Hour_Alterations_Index_URL, headers: headers, success: { (json) in
print(json)
DispatchQueue.main.async {
self.hideSpinner()
let swiftyJsonVar = JSON(json)
// Alteration array data
let myAlterationsVar = swiftyJsonVar["my_alterations_to_others"].rawString()!
let myAlterationsjsonData = myAlterationsVar.data(using: .utf8)!
let myAlterationsDic = try? JSONSerialization.jsonObject(with: myAlterationsjsonData, options: []) as! Array< Any>
for myAlter in myAlterationsDic! {
let totlDic = NSMutableDictionary()
let dic = myAlter as! Dictionary<String,Any>
// totlDic.setValue(dic["day_order"]!, forKey: "day_order")
totlDic.setValue(dic["batch"]!, forKey: "batch")
totlDic.setValue(dic["semester"]!, forKey: "semester")
totlDic.setValue(dic["section"]!, forKey: "section")
totlDic.setValue(dic["subject_altered"]!, forKey: "subject_altered")
totlDic.setValue(dic["subject_handled"]!, forKey: "subject_handled")
totlDic.setValue(dic["timing"]!, forKey: "timing")
totlDic.setValue(dic["date"]!, forKey: "date")
totlDic.setValue(dic["altered_to"]!, forKey: "altered_to")
self.myAlterationsArray.add(totlDic)
print("myAlterationsArray--",self.myAlterationsArray )
}
self.myAlterationsListView.reloadData()
// department_wise_alterations array data
let departmentVar = swiftyJsonVar["department_wise_alterations"].rawString()!
let departmentjsonData = departmentVar.data(using: .utf8)!
let departmentDic = try? JSONSerialization.jsonObject(with: departmentjsonData, options: []) as! Array< Any>
for departments in departmentDic! {
let totlDic = NSMutableDictionary()
let dic = departments as! Dictionary<String,Any>
totlDic.setValue(dic["day_order"]!, forKey: "day_order")
totlDic.setValue(dic["batch"]!, forKey: "batch")
totlDic.setValue(dic["semester"]!, forKey: "semester")
totlDic.setValue(dic["section"]!, forKey: "section")
totlDic.setValue(dic["subject_altered"]!, forKey: "subject_altered")
totlDic.setValue(dic["subject_handled"]!, forKey: "subject_handled")
totlDic.setValue(dic["timing"]!, forKey: "timing")
totlDic.setValue(dic["date"]!, forKey: "date")
totlDic.setValue(dic["altered_to"]!, forKey: "altered_to")
self.departmentArray.add(totlDic)
print("departmentArray--",self.departmentArray )
}
self.othersListView.reloadData()
// my_alteration_from_others array data
let othersalterationVar = swiftyJsonVar["my_alteration_from_others"].rawString()!
let othersjsonData = othersalterationVar.data(using: .utf8)!
let othersDic = try? JSONSerialization.jsonObject(with: othersjsonData, options: []) as! Array< Any>
for othersdata in othersDic! {
let totlDic = NSMutableDictionary()
let dic = othersdata as! Dictionary<String,Any>
totlDic.setValue(dic["day_order"]!, forKey: "day_order")
totlDic.setValue(dic["batch"]!, forKey: "batch")
totlDic.setValue(dic["semester"]!, forKey: "semester")
totlDic.setValue(dic["section"]!, forKey: "section")
totlDic.setValue(dic["subject_altered"]!, forKey: "subject_altered")
totlDic.setValue(dic["subject_handled"]!, forKey: "subject_handled")
totlDic.setValue(dic["timing"]!, forKey: "timing")
totlDic.setValue(dic["date"]!, forKey: "date")
totlDic.setValue(dic["altered_to"]!, forKey: "altered_to")
self.othersArray.add(totlDic)
print("othersArray--",self.othersArray )
}
self.departmentListView.reloadData()
} } , failure: { (error) in
//error code
print(error)
})
}
}
// MARK:- UICollectionView DataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.othersListView {
return othersArray.count
}
else if collectionView == self.departmentListView
{
return departmentArray.count
}
return myAlterationsArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
var cell: UICollectionViewCell!
if collectionView == self.myAlterationsListView {
let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "SKMyAlterationsCell", for: indexPath) as! SKMyAlterationsCell
let localDic = myAlterationsArray.object(at: indexPath.row) as? NSDictionary
cell1.dayOredrLbl.text = localDic?.value(forKey: "day_order") as? String
cell1.batchLbl.text = localDic?.value(forKey: "batch") as? String
cell1.semLbl.text = localDic?.value(forKey: "semester") as? String
cell1.secLbl.text = localDic?.value(forKey: "section") as? String
cell1.subjAlteredLbl.text = localDic?.value(forKey: "subject_altered") as? String
cell1.subjHandledLbl.text = localDic?.value(forKey: "subject_handled") as? String
cell1.timingLbl.text = localDic?.value(forKey: "timing") as? String
cell1.dateLbl.text = localDic?.value(forKey: "date") as? String
cell1.AlteredToLbl.text = localDic?.value(forKey: "altered_to") as? String
return cell1
}
else if collectionView == self.othersListView
{
let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "SKMyAlterationsCell", for: indexPath) as! SKMyAlterationsCell
let localDic = othersArray.object(at: indexPath.row) as? NSDictionary
cell1.dayOredrLbl.text = localDic?.value(forKey: "day_order") as? String
cell1.batchLbl.text = localDic?.value(forKey: "batch") as? String
cell1.semLbl.text = localDic?.value(forKey: "semester") as? String
cell1.secLbl.text = localDic?.value(forKey: "section") as? String
cell1.subjAlteredLbl.text = localDic?.value(forKey: "subject_altered") as? String
cell1.subjHandledLbl.text = localDic?.value(forKey: "subject_handled") as? String
cell1.timingLbl.text = localDic?.value(forKey: "timing") as? String
cell1.dateLbl.text = localDic?.value(forKey: "date") as? String
cell1.AlteredToLbl.text = localDic?.value(forKey: "altered_to") as? String
return cell1
}
else
{
let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "SKMyAlterationsCell", for: indexPath) as! SKMyAlterationsCell
let localDic = departmentArray.object(at: indexPath.row) as? NSDictionary
cell1.dayOredrLbl.text = localDic?.value(forKey: "day_order") as? String
cell1.batchLbl.text = localDic?.value(forKey: "batch") as? String
cell1.semLbl.text = localDic?.value(forKey: "semester") as? String
cell1.secLbl.text = localDic?.value(forKey: "section") as? String
cell1.subjAlteredLbl.text = localDic?.value(forKey: "subject_altered") as? String
cell1.subjHandledLbl.text = localDic?.value(forKey: "subject_handled") as? String
cell1.timingLbl.text = localDic?.value(forKey: "timing") as? String
cell1.dateLbl.text = localDic?.value(forKey: "date") as? String
cell1.AlteredToLbl.text = localDic?.value(forKey: "altered_to") as? String
return cell1
}
return cell
}
}
//MARK:- collectionViewCell Nibs
func loadNibs()
{
//MARK:- SKSubmissAssigmCell
let nib = UINib(nibName: "SKMyAlterationsCell", bundle: nil)
myAlterationsListView?.register(nib, forCellWithReuseIdentifier: "SKMyAlterationsCell")
if let flowLayout = myAlterationsListView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
}
myAlterationsListView.dataSource = self
let nib2 = UINib(nibName: "SKMyAlterationsCell", bundle: nil)
othersListView?.register(nib2, forCellWithReuseIdentifier: "SKMyAlterationsCell")
if let flowLayout = othersListView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
}
othersListView.dataSource = self
let nib3 = UINib(nibName: "SKMyAlterationsCell", bundle: nil)
departmentListView?.register(nib3, forCellWithReuseIdentifier: "SKMyAlterationsCell")
if let flowLayout = departmentListView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
}
departmentListView.dataSource = self
}
这是我的collectionview单元格代码。
import UIKit
class SKMyAlterationsCell: UICollectionViewCell{
@IBOutlet weak var dayOredrLbl:UILabel!
@IBOutlet weak var batchLbl:UILabel!
@IBOutlet weak var semLbl:UILabel!
@IBOutlet weak var secLbl:UILabel!
@IBOutlet weak var subjAlteredLbl:UILabel!
@IBOutlet weak var subjHandledLbl:UILabel!
@IBOutlet weak var timingLbl:UILabel!
@IBOutlet weak var dateLbl:UILabel!
@IBOutlet weak var AlteredToLbl:UILabel!
@IBOutlet weak var subview:UIView!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
self.subview.layer.cornerRadius = 5.0
self.contentView.translatesAutoresizingMaskIntoConstraints = false
let screenWidth = UIScreen.main.bounds.size.width
widthConstraint.constant = screenWidth - (2 * 12)
// Initialization code
}
}
答案 0 :(得分:1)
我已经完成了你的代码一切都很好,我想你正在做动态单元格高度。删除单元格并创建新单元格,确保标签应具有顶部,底部,左侧和右侧的约束。然后清理并运行它必须工作。