使用 NativeExpress admob 的 didSelectRowAtIndexPath 中的错误索引
Wrong index in didSelectRowAtIndexPath using NativeExpress admob
'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 43 beyond bounds [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()
中更改什么吗?
似乎 dataArray
是 40 个常规单元格的数据源。如果是,则广告单元格的数据源将丢失。如果不是,那么我看不到任何区分常规内容和广告内容的逻辑。
最后,整个 if
声明似乎没有太大意义。尽管您试图区分广告和常规单元格,但 then
和 else
分支执行的操作完全相同。请让我知道你到底想实现什么。
请 return 你数组 (dataArray)
计数然后你永远不会崩溃
func tableView(tableView:UITableView!, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
感谢大家。我的问题是通过在 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)
{
"$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;
}
'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 43 beyond bounds [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()
中更改什么吗?
似乎 dataArray
是 40 个常规单元格的数据源。如果是,则广告单元格的数据源将丢失。如果不是,那么我看不到任何区分常规内容和广告内容的逻辑。
最后,整个 if
声明似乎没有太大意义。尽管您试图区分广告和常规单元格,但 then
和 else
分支执行的操作完全相同。请让我知道你到底想实现什么。
请 return 你数组 (dataArray)
计数然后你永远不会崩溃
func tableView(tableView:UITableView!, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
感谢大家。我的问题是通过在 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)
{
"$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;
}