didSelectRowAtIndexPath中使用NativeExpress admob的索引错误

时间:2017-05-03 13:07:11

标签: ios swift didselectrowatindexpath google-admob

  

' 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 个答案:

答案 0 :(得分:1)

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

最后,整个if声明似乎没有多大意义。虽然您尝试区分广告和常规单元格,但thenelse分支执行相同的操作。请让我知道你想要达到的目的。

答案 1 :(得分:1)

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

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

答案 2 :(得分: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;
}

答案 3 :(得分: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)