在我的表格视图中设置单元格时,我遇到了这个问题:
日志:
*由于未捕获的异常终止应用' NSRangeException',原因:' * - [__ NSArrayM objectAtIndex:]:超出边界的索引5 [0 .. 4] ' ***第一次抛出调用堆栈: (0x18696afd8 0x1853cc538 0x1868491f0 0x100918a30 0x10091960c 0x18cddf114 0x18cddf32c 0x18cdcca30 0x18cde431c 0x18cb7c92c 0x18ca97158 0x189c87274 0x189c7bde8 0x189c7bca8 0x189bf7360 0x189c1e3c0 0x189c1ee8c 0x1869189a0 0x186916628 0x186846db4 0x1882b0074 0x18caff130 0x1006920bc 0x18585559c) libc ++ abi.dylib:以NSException类型的未捕获异常终止
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (tableView == tableview) {
let cell = tableView.dequeueReusableCell(withIdentifier: "table", for: indexPath as IndexPath) as! homeTableViewCell
if (titlearray.count != 0) {
let tit: NSString = self.titlearray[indexPath.row] as! NSString
print("titlearraytitlearray\(titlearray)")
appDelegate.trendseetitarray = titlearray
print("appDelegatetrendseearray\(appDelegate.trendseetitarray)")
cell.treadingtitle.text! = tit as String
}
if (pricearray.count != 0) {
let tit: NSString = self.pricearray[indexPath.row] as! NSString
appDelegate.trendseepricearray = pricearray
cell.trendingprice.text! = tit as String
}
if (addressarraylist.count != 0) {
let tit: NSString = self.addressarraylist[indexPath.row] as! NSString
appDelegate.addressarraylist = addressarraylist
}
if (imagearray.count != 0 && imagearray.count != nil ) {
print(imagearray)
let roomurl=URL(string: "\(imagearray[indexPath.row])")
// print("Room URL:\(roomurl!)")
appDelegate.trendseeimagearray = imagearray
cell.Trendingimg.sd_setImage(
with: roomurl,
placeholderImage: UIImage(named:"no_image.png"),
options: SDWebImageOptions.refreshCached,
completed: { (image1: UIImage?, error1: Error?, cacheType1: SDImageCacheType, imageURL1: URL?) -> Void in
}
)
cell.Trendingimg.clipsToBounds = true;
cell.Trendingimg.sd_setImage(with: roomurl, completed: self.block1)
}
return cell
}
return UITableViewCell()
}
错误:
if (addressarraylist.count != 0) {
let tit: NSString = self.addressarraylist[indexPath.row] as! NSString
appDelegate.addressarraylist = addressarraylist
}
问题图片:
答案 0 :(得分:0)
为什么要检查Array
的大小是否为非零而不是测试计数是否至少与indexPath.row
一样大?试试这个:
// array index must be less than the count since the index is zero-based
if indexPath.row < addressarraylist.count {
let tit: NSString = addressarraylist[indexPath.row] as! NSString
appDelegate.addressarraylist = addressarraylist
}
在解开它们之前,您还应测试Optional
值。强制解包可能导致运行时崩溃。像这样测试它们:
if let tit: NSString = addressarraylist[indexPath.row] as? NSString { ... }
答案 1 :(得分:0)
你可以试试这个: -
if self.addressarraylist.count > indexPath.row {
let tit: NSString = self.addressarraylist[indexPath.row] as! NSString
appDelegate.addressarraylist = addressarraylist
}