1

'NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:索引 43 超出范围 [0 .. 39]'

在下面的代码中,为什么总是else执行该块,而在其中使用相同的代码CellForRowAtIndexPath却可以正常工作?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let tableItem = dataArray[indexPath.row] as? GADNativeExpressAdView {
        valueToPass = indexPath.row
        self.performSegueWithIdentifier("showDetails", sender: self)
    } else {
        valueToPass = indexPath.row
        self.performSegueWithIdentifier("showDetails", sender: self)
    }
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if let nativeExpressAdView = dataArray[indexPath.row] as? GADNativeExpressAdView {
        let reusableAdCell = tableView.dequeueReusableCellWithIdentifier("NativeExpressAdViewCell", forIndexPath: indexPath)

        // Remove previous GADNativeExpressAdView from the content view before adding a new one.
        for subview in reusableAdCell.contentView.subviews {
            subview.removeFromSuperview()
        }

        reusableAdCell.contentView.addSubview(nativeExpressAdView)
        // Center GADNativeExpressAdView in the table cell's content view.
        nativeExpressAdView.center = reusableAdCell.contentView.center

        return reusableAdCell

    }
    else {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? ProductCell
        dataDict = dataArray[indexPath.row] as! NSMutableDictionary
        cell!.productName?.text = dataDict.objectForKey("name") as? String
        let member : NSString
        member = (dataDict.objectForKey("Member") as? String)!
        cell!.productMemberName?.text = "Member: " + (member as String)
        cell!.productFavorite?.setBackgroundImage(UIImage(named: ""), forState: UIControlState.Normal)
        cell!.productImage?.image = UIImage(named: (dataDict.objectForKey("name") as? String)! + ".jpg")
        let imageName = "cellArrow"
        let image = UIImage(named: imageName)
        let imageView = UIImageView(image: image!)
        cell?.accessoryView = imageView
        return cell!
    }
}

知道我应该改变什么didSelectRowAtIndexPath()吗?

4

4 回答 4

1

看起来好像dataArray是 40 个常规单元格的数据源。如果是,则广告单元的数据源将丢失。如果不是,那么我看不到任何区分常规内容和广告内容的逻辑。

最后,整个if声明似乎没有太多意义。尽管您尝试区分广告和常规单元格then,但分支和else分支都执行相同的操作。请让我知道您到底想达到什么目标。

于 2017-05-03T13:22:48.033 回答
1

请返回您的数组(dataArray)计数,然后您将永远不会崩溃

func tableView(tableView:UITableView!, numberOfRowsInSection section: Int) -> Int {
     return dataArray.count
}
于 2017-05-03T13:28:21.413 回答
1
{
    "$id" = 1;
    Data =     (
                {
            "$id" = 2;
            Birthdate = "11-12-2017";
            EmailId = "h@gmail.com";
            Gender = Male;
            Latitude = "23.0053134";
            Longitude = "72.5553861";
            MobileNo = 8494;
            Name = hu;
            RegistrationId = 98;
        },


                {
            "$id" = 11;
            Birthdate = "07-06-1991";
            EmailId = "divya@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 4545646546;
            Name = divya;
            RegistrationId = 89;
        },
                {
            "$id" = 12;
            Birthdate = "07-06-1991";
            EmailId = "jini@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 4545484654;
            Name = gini;
            RegistrationId = 88;
        },
                {
            "$id" = 13;
            Birthdate = "20-12-2017";
            EmailId = "mehta3430@gmail.com";
            Gender = Male;
            Latitude = "18.4457046";
            Longitude = "73.9197953";
            MobileNo = 8888867676;
            Name = "Mukesh Mehta";
            RegistrationId = 87;
        },
                {
            "$id" = 14;
            Birthdate = "07-06-1991";
            EmailId = "dvgz@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 4465456;
            Name = fbgfdsgbdf;
            RegistrationId = 86;
        },
                {
            "$id" = 15;
            Birthdate = "07-06-1991";
            EmailId = "hini@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 7874777564;
            Name = gfuisb;
            RegistrationId = 85;
        },
                {
            "$id" = 16;
            Birthdate = "07-06-1991";
            EmailId = "jini@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 4485465448;
            Name = jini;
            RegistrationId = 84;
        },
                {
            "$id" = 17;
            Birthdate = "07-06-1991";
            EmailId = "hini@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 5468746465;
            Name = hini;
            RegistrationId = 83;
        },
                {
            "$id" = 18;
            Birthdate = "07-06-1991";
            EmailId = "hini@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 5468746465;
            Name = hini;
            RegistrationId = 82;
        },
                {
            "$id" = 19;
            Birthdate = "07-06-1991";
            EmailId = "hini@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 5468746465;
            Name = hini;
            RegistrationId = 81;
        },
                {
            "$id" = 20;
            Birthdate = "07-06-1991";
            EmailId = "jini@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 5274532743;
            Name = fgfh;
            RegistrationId = 80;
        },
                {
            "$id" = 21;
            Birthdate = "07-06-1991";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = "<null>";
            Name = ss;
            RegistrationId = 79;
        },
                {
            "$id" = 22;
            Birthdate = "07-06-1991";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = "<null>";
            Name = ss;
            RegistrationId = 78;
        },
                {
            "$id" = 23;
            Birthdate = "07-06-1991";
            EmailId = "sagarzala123@gmail.com";
            Gender = Male;
            Latitude = "23.4561466";
            Longitude = "72.6565656";
            MobileNo = 9730209865;
            Name = Sagar;
            RegistrationId = 77;
        },
                {
            "$id" = 24;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = Sagar;
            RegistrationId = 76;
        },
                {
            "$id" = 25;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = Sagar;
            RegistrationId = 75;
        },
                {
            "$id" = 26;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = hdjhsa;
            RegistrationId = 74;
        },
                {
            "$id" = 27;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = hdjhsa;
            RegistrationId = 73;
        },
                {
            "$id" = 28;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = hdjhsa;
            RegistrationId = 72;
        },
                {
            "$id" = 29;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = hdjhsa;
            RegistrationId = 71;
        },
                {
            "$id" = 30;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = hdjhsa;
            RegistrationId = 70;
        },
                {
            "$id" = 31;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = hdjhsa;
            RegistrationId = 69;
        },
                {
            "$id" = 32;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Female;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = hdjhsa;
            RegistrationId = 68;
        },
                {
            "$id" = 33;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Female;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = hdjhsa;
            RegistrationId = 67;
        },
                {
            "$id" = 34;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Female;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = jin;
            RegistrationId = 66;
        },
                {
            "$id" = 35;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Female;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = kinjal;
            RegistrationId = 65;
        },
                {
            "$id" = 36;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = sagar;
            RegistrationId = 64;
        },
                {
            "$id" = 37;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = sagar;
            RegistrationId = 63;
        },
                {
            "$id" = 38;
            Birthdate = "<null>";
            EmailId = "<null>";
            Gender = Male;
            Latitude = "<null>";
            Longitude = "<null>";
            MobileNo = "<null>";
            Name = asxa;
            RegistrationId = 62;
        },
                {
            "$id" = 39;
            Birthdate = "13-12-2017";
            EmailId = "test@shd.com";
            Gender = Male;
            Latitude = "23.0053101";
            Longitude = "72.5553988";
            MobileNo = 919714629;
            Name = test;
            RegistrationId = 61;
        },
                {
            "$id" = 40;
            Birthdate = "18-12-2017";
            EmailId = "sagar@gmail.com";
            Gender = Male;
            Latitude = "23.0052967";
            Longitude = "72.5553918";
            MobileNo = 123;
            Name = s;
            RegistrationId = 60;
        },
                {
            "$id" = 41;
            Birthdate = "17-12-2017";
            EmailId = "adg@gmail.com";
            Gender = Male;
            Latitude = "0.0";
            Longitude = "0.0";
            MobileNo = 8569856668;
            Name = "multiple test";
            RegistrationId = 59;
        },
                {
            "$id" = 42;
            Birthdate = "05-03-2017";
            EmailId = "a@a.com";
            Gender = "";
            Latitude = "26.3443";
            Longitude = "26.3443";
            MobileNo = 1234567890;
            Name = Ashish;
            RegistrationId = 58;
        },

    );
    Message = "List generated successfully";
    Status = 1;
}
于 2018-01-06T12:48:01.387 回答
0

谢谢大家。我的问题通过在 didselect() 中执行此操作得到解决

 let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("FirstDetailViewController") as! FirstDetailViewController
    dataDict = dataArray.objectAtIndex(indexPath.row) as! NSMutableDictionary
    let mainPk: String? = dataDict.objectForKey("mainpk") as? String
    let finalIndex = Int((mainPk)!)
    secondViewController.selectedIndex = finalIndex! - 1
    self.navigationController!.pushViewController(secondViewController, animated: true)
于 2017-05-04T06:55:24.383 回答