gpt4 book ai didi

ios - swift 2 : MBProgressHUD Refresh later Error : Terminating app due to uncaught exception 'NSRangeException'

转载 作者:行者123 更新时间:2023-11-28 08:57:42 25 4
gpt4 key购买 nike

我的代码在第一次打开 View Controller 时运行成功,但稍后刷新时出现错误:

 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:

我的代码在这里

import UIKit

class RecipeViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var tableview: UITableView?
@IBOutlet weak var titleLabel: UILabel?
var dataArray:NSMutableArray!
var recipeId:NSString!
var titleString:NSString!
var pagingCount:NSInteger!

var isFromSearch:Bool!

override func viewDidLoad() {
super.viewDidLoad()


titleLabel?.text = titleString as String
pagingCount = 0
self.loadMore()

}

func loadMore()
{
if (self.isFromSearch == false)
{
self.dataArray = NSMutableArray()
let connection : APIConnection = APIConnection();
MBProgressHUD.showHUDAddedTo(self.view, animated: true)
connection.startRequest(String(format:"%@getRecipeByCuisineId.php?cuisine_id=%@&page=%d&count=%@",kServerURl,recipeId,pagingCount,kRecipeCount), responseBlock: { (data :[AnyObject]!) -> Void in

let jsonResult: NSArray = data as NSArray;
if((jsonResult.valueForKey("success").boolValue) == true)
{
self.dataArray.addObjectsFromArray(jsonResult.valueForKey("recipe") as! NSMutableArray as [AnyObject])
NSLog("%d",self.dataArray.count);
self.tableview?.reloadData();
MBProgressHUD.hideHUDForView(self.view, animated: true)
}
else
{

MBProgressHUD.hideHUDForView(self.view, animated: true)
}

}) { (error : NSError!) -> Void in
MBProgressHUD.hideHUDForView(self.view, animated: true)
}
}
}


override func viewWillAppear(animated: Bool) {
//self.navigationController?.navigationBarHidden(false)
self.navigationController?.navigationBar.hidden = true
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if ((dataArray) != nil)
{
return dataArray.count;
}
return 0;
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

if UIDevice.currentDevice().userInterfaceIdiom == .Pad
{
return 200;
}
else
{
return 160.0;
}

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let identifier = NSString(format:"%@%d","cell",indexPath.row)

var cell:RecipeCell? =
tableview?.dequeueReusableCellWithIdentifier(identifier as String) as? RecipeCell

if (cell == nil)
{
cell = RecipeCell(style: UITableViewCellStyle.Default, reuseIdentifier:identifier as String)
}

cell?.recipeId = dataArray.objectAtIndex(indexPath.row).valueForKey("recipeid") as? String
cell?.cuisinesId = dataArray.objectAtIndex(indexPath.row).valueForKey("cuisineid") as? String
cell?.lblRecipeName?.text = dataArray.objectAtIndex(indexPath.row).valueForKey("recipename") as? String
cell?.lblRecipeTime?.text = dataArray.objectAtIndex(indexPath.row).valueForKey("prep_time") as? String

let str = String(format:"%@%@",kDownloadCategoryImage,dataArray.objectAtIndex(indexPath.row).valueForKey("recipe_image") as! String)
let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: NSURL!) -> Void in

}

cell?.recipeImage!.sd_setImageWithURL(NSURL(string: str), completed: block)

return cell!
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let cell:RecipeCell? = tableview?.cellForRowAtIndexPath(indexPath) as? RecipeCell

let recipeDiscriptionVC = self.storyboard?.instantiateViewControllerWithIdentifier("recipeDiscriptionVC") as! RecipeDiscriptionViewController
recipeDiscriptionVC.isFromFavorite = false
recipeDiscriptionVC.recipeId = cell?.recipeId
recipeDiscriptionVC.titleString = dataArray.objectAtIndex(indexPath.row).valueForKey("recipename") as? String
recipeDiscriptionVC.recipeName = dataArray.objectAtIndex(indexPath.row).valueForKey("recipename") as? String
recipeDiscriptionVC.recipeDesc = dataArray.objectAtIndex(indexPath.row).valueForKey("recipe_desc") as? String
recipeDiscriptionVC.recipeIngredients = dataArray.objectAtIndex(indexPath.row).valueForKey("recipe_ingredients") as? String
recipeDiscriptionVC.prepTime = dataArray.objectAtIndex(indexPath.row).valueForKey("prep_time") as? String
recipeDiscriptionVC.cookingTime = dataArray.objectAtIndex(indexPath.row).valueForKey("cooking_time") as? String
recipeDiscriptionVC.method = dataArray.objectAtIndex(indexPath.row).valueForKey("Method") as? String
recipeDiscriptionVC.recipeImage = dataArray.objectAtIndex(indexPath.row).valueForKey("recipe_image") as? String
recipeDiscriptionVC.videoLink = dataArray.objectAtIndex(indexPath.row).valueForKey("video_link") as? String
recipeDiscriptionVC.ratings = dataArray.objectAtIndex(indexPath.row).valueForKey("ratings") as? String
recipeDiscriptionVC.ratingCounter = dataArray.objectAtIndex(indexPath.row).valueForKey("rating_counter") as? String

self.navigationController?.pushViewController(recipeDiscriptionVC, animated: true)
}

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {

let currentOffset = scrollView.contentOffset.y;
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

if (maximumOffset - currentOffset <= 10.0) {
// For Pagging
pagingCount = pagingCount + 1
self.loadMore()

}

}


override func viewWillDisappear(animated: Bool) {
self.navigationController?.navigationBar.hidden = false
}


@IBAction func btnBackTap(sender: UIButton)
{
self.navigationController?.popViewControllerAnimated(true)
}

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


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

而且我认为错误区域在这里,当稍后刷新时 click item 给出空数组错误。我试过了,但我没有解决它。

func loadMore()
{
if (self.isFromSearch == false)
{
self.dataArray = NSMutableArray()
let connection : APIConnection = APIConnection();
MBProgressHUD.showHUDAddedTo(self.view, animated: true)
connection.startRequest(String(format:"%@getRecipeByCuisineId.php?cuisine_id=%@&page=%d&count=%@",kServerURl,recipeId,pagingCount,kRecipeCount), responseBlock: { (data :[AnyObject]!) -> Void in

let jsonResult: NSArray = data as NSArray;
if((jsonResult.valueForKey("success").boolValue) == true)
{
self.dataArray.addObjectsFromArray(jsonResult.valueForKey("recipe") as! NSMutableArray as [AnyObject])
NSLog("%d",self.dataArray.count);
self.tableview?.reloadData();
MBProgressHUD.hideHUDForView(self.view, animated: true)
}
else
{

MBProgressHUD.hideHUDForView(self.view, animated: true)
}

}) { (error : NSError!) -> Void in
MBProgressHUD.hideHUDForView(self.view, animated: true)
}
}
}

请帮帮我。

最佳答案

请使用 svprogresshud 而不是 mbprogresshud

链接:https://github.com/TransitApp/SVProgressHUD

关于ios - swift 2 : MBProgressHUD Refresh later Error : Terminating app due to uncaught exception 'NSRangeException' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32668351/

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