【问题标题】:Json - Prepare for segue to Details ViewJson - 准备转至详细信息视图
【发布时间】:2016-04-13 17:53:54
【问题描述】:

我正在尝试使用 Json 中的详细信息元素对视图进行编码。但是编译器在为 segue 做准备时崩溃。

@IBOutlet weak var menuButton:UIBarButtonItem!

let yourJsonFormat: String = "JSONFile" // set text JSONFile : json data from file
                                        // set text JSONUrl : json data from web url

var arrDict :NSMutableArray=[]

// @IBOutlet weak var eventCalendar: UITableView!

override func viewDidLoad()
{
    super.viewDidLoad()

    if self.revealViewController() != nil {
        menuButton.target = self.revealViewController()
        menuButton.action = "revealToggle:"
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }

    if yourJsonFormat == "JSONFile" {
        jsonParsingFromFile()
    } else {
        jsonParsingFromURL()
    }
}


func jsonParsingFromURL () {

    let url = NSURL(string: "http://theappguruz.in//Apps/iOS/Temp/json.php")
    let request = NSURLRequest(URL: url!)

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
        self.startParsing(data!)
    }
}


func jsonParsingFromFile()
{
    let path: NSString = NSBundle.mainBundle().pathForResource("days", ofType: "json")!
    let data : NSData = try! NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMapped)

    self.startParsing(data)
}

func startParsing(data :NSData)
{
    let dict: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary

    for var i = 0 ; i < (dict.valueForKey("objects") as! NSArray).count ; i++ {
        arrDict.addObject((dict.valueForKey("objects") as! NSArray) .objectAtIndex(i))
    }
}

// eventCalendar .reloadData()

override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return 1
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return arrDict.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableViewCell

    let strName : NSString=arrDict[indexPath.row] .valueForKey("name") as! NSString
    let strSubtitle : NSString=arrDict[indexPath.row] .valueForKey("subtitle") as! NSString
    let strLocation : NSString=arrDict[indexPath.row] .valueForKey("location") as! NSString
    let strStart : NSString=arrDict[indexPath.row] .valueForKey("start") as! NSString

    if let strImage = arrDict[indexPath.row] .valueForKey("image") as? String {

        if let data = NSData(contentsOfURL: NSURL(string:strImage )!) {
            cell.lbImage.image = UIImage(data: data)
        }
    }

    cell.lbName.text=strName as String
    cell.lbSubtitle.text=strSubtitle as String
    cell.lbStart.text=strStart as String
    cell.lbLocation.text=strLocation as String

    return cell as TableViewCell
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var detailView = segue.destinationViewController as! EventDetail

    if segue.identifier == EventDetail.identifier() {

        if let indexPath = self.tableView.indexPathForSelectedRow {
            let selectedRow = indexPath.row
            let selected = self.arrDict[selectedRow]
            let vc = segue.destinationViewController as! EventDetail
            vc.ARRDICT = selected as! NSMutableArray
        }
    }
}

错误信息是:

由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array”

【问题讨论】:

  • 请添加完整的代码。例如arrDict 的结构。为什么不在发件人中发送信息?
  • 我把代码... 为什么你认为发件人更好?谢谢。
  • 您是否在情节提要中添加了“showEventDetail”转场标识符?如果没有,打开故事板,找到你的 segue 并用'showEventDetail' id 识别它。
  • 是的,我在storyBoard中添加了showEventDetail。错误消息是:由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array”
  • 代码很混乱。看来startParsing函数的第二部分实际上属于cellForRowAtIndexPath,而您粘贴了三遍完全相同的for循环。

标签: ios json swift detailsview


【解决方案1】:

仅用于测试,您应该删除情节提要中的自动转场并添加手动转场(类型显示),使用标识符并在didSelectRowAtIndexPath 中调用self.performSegueWithIdentifier("yourIdentifier", sender: sender) 此方法有indexPath 使用此索引路径获取位置数组并尝试将该对象作为 sender 参数发送到该位置。并在prepareForSegue 中获取。

请将您的 startParsing 循环更改为。

let arrayObjects = dict.valueForKey("objects") as! NSArray
for eachDict in arrayObjects {
  let dict = eachDict as! NSDictionary
  arrDict.addObject(dict)
}

为了更好level of complexity

【讨论】:

    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多