gpt4 book ai didi

ios - 数组索引超出范围 Query 和 CollectionCell

转载 作者:行者123 更新时间:2023-11-29 01:22:25 24 4
gpt4 key购买 nike

出于某种原因,我无法弄清楚我做错了什么:

我正在尝试在我的解析数据库中查询 URL,将它们放入名为 facebookPics 的数组中。稍后我想在我的 cellForRowAtIndexPath 中进行检查,

if facebookPics.count < indexPath.row

然后在我的 CollectionViewCells 中加载数据。 => 我每次正好有 6 个。

代码:

 override func viewWillAppear(animated: Bool) {
let infoQuery = PFUser.query()
infoQuery?.whereKey("objectId", equalTo: objectID!)

infoQuery?.findObjectsInBackgroundWithBlock({ (results, error) -> Void in
if error != nil{

let userMessage = error!.localizedDescription
let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)

return
}
else if results != nil{

for result in results! {


let imageFile = result["firstImage"] as! PFFile
// Image muss ja erstmal gedownloaded werden.
imageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in

if error != nil {
let userMessage = error!.localizedDescription
let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)

return

} else {
if let data = imageData {

self.miniImage.image = UIImage(data: data)

}

}

}
// Jetzt der firstname, birthday und abouttext
var profilText = result["first_name"] as! String
self.nameAgeLabel.text = profilText

if let geburtstag = result["birthday"] as? String {

let calendar = NSCalendar.currentCalendar()
let currentDate = NSDate()
let birthdayFormatter = NSDateFormatter()
birthdayFormatter.dateFormat = "MM/DD/YYYY"
let birthday = birthdayFormatter.dateFromString(geburtstag)



let ageComponents = calendar.components(.Year,
fromDate: birthday!,
toDate: currentDate,
options: [])


let age = ageComponents.year


profilText += ", " + String(age)
self.nameAgeLabel.text = profilText


}


if let hashtags = result["hashtags"] as? String {

self.hashtagText.text = hashtags

}

if let images = result["images"] as? [String] {



for image in images {

let nsURL = NSURL(string: image)
var urls:[NSURL] = [NSURL]()
urls.append(nsURL!)
self.facebookPics = urls

}

print(self.facebookPics)


}






}

self.mainCollectionView.reloadData()


}
})

}


override func viewDidLoad() {
super.viewDidLoad()


// Do any additional setup after loading the view.
let image : UIImage = UIImage(named: "Überschrift.png")!
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 80, height: 30))
imageView.contentMode = .ScaleAspectFit
imageView.image = image
self.navigationItem.titleView = imageView

// Navigation Controller Top soll Durchsichtig sein
self.navigationController?.navigationBar.opaque = true

miniImage.layer.borderWidth = 0.5
miniImage.layer.masksToBounds = false
miniImage.layer.borderColor = UIColor.darkGrayColor().CGColor
miniImage.layer.cornerRadius = miniImage.frame.width/2
miniImage.clipsToBounds = true

hashtagText.layer.borderWidth = 1.5
hashtagText.layer.borderColor = UIColor.darkTextColor().CGColor
hashtagText.layer.cornerRadius = 7







// Suche starten um folgende Infos zu bekommen [Profilbild, firstname, abouttext, andere Bilder adressen.]





}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}




func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

// Alle images.count => returnen! max 6



return 6



}
//FEHLERHAFTE FUNKTION



func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let collectionCell:SettingsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as! SettingsCollectionViewCell

collectionView.backgroundColor = UIColor.whiteColor()


collectionCell.backgroundColor = UIColor.whiteColor()
collectionCell.frame.size.width = 80
collectionCell.frame.size.height = 80
collectionCell.layer.borderWidth = 1
collectionCell.layer.borderColor = UIColor.darkGrayColor().CGColor
collectionCell.layer.cornerRadius = collectionCell.frame.size.width/2




if facebookPics.count < indexPath.row{

if let data = NSData(contentsOfURL: facebookPics[indexPath.row]) {
let image = UIImage(data: data)
collectionCell.collectionViewImage.image = image
}
}

else {
// Clear out the image on a recycled cell
collectionCell.collectionViewImage = nil
}


let cellImage = collectionCell.collectionViewImage

if cellImage == nil {

self.counter = 1
print("empty")
collectionCell.collectionViewButton.center.x = 40
collectionCell.collectionViewButton.center.y = 40
collectionCell.collectionViewButton.setImage(UIImage(named: "check.png"), forState: UIControlState.Normal)



}
else {

self.counter = 0
print("hello")
collectionCell.collectionViewButton.center.x = 60
collectionCell.collectionViewButton.center.y = 60
collectionCell.collectionViewButton.setImage(UIImage(named: "close-o.png"), forState: UIControlState.Normal)
collectionCell.collectionViewButton.tintColor = UIColor.redColor()
}







return collectionCell
}

最佳答案

您必须反转 if 条件

如果您有 0 张图片并尝试显示第 3 张图片,您当前的代码将进入 if block ,因为 0 < 3 的计算结果为真。这与你想要的完全相反。更一般地说,当且仅当索引正好超出范围时,您目前才进入 true-branch。

正确的检查是:

if indexPath.row < facebookPics.count {
// logic
}

关于ios - 数组索引超出范围 Query 和 CollectionCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34434328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com