我有一个tableViewController,它有4个单元格。每个单元格都是从viewDidLoad()中的xib文件加载的:
override func viewDidLoad() {
super.viewDidLoad()
let itemPreBasketServer = ItemPreBasketServer()
let searchForIDServer = SearchForIDServer()
let descCell = UINib(nibName: "PreDescTableViewCell", bundle: nil)
tableView.register(descCell, forCellReuseIdentifier: "descCell")
let priceCell = UINib(nibName: "PrePriceTableViewCell", bundle: nil)
tableView.register(priceCell, forCellReuseIdentifier: "priceCell")
let quantityCell = UINib(nibName: "PreQuantityTableViewCell", bundle: nil)
tableView.register(quantityCell, forCellReuseIdentifier: "quantityCell")
let submitCell = UINib(nibName: "PreSubmitTableViewCell", bundle: nil)
tableView.register(submitCell, forCellReuseIdentifier: "submitCell")
if incomeName != "" {
characterSet.insert(charactersIn: "=\"#%/<>?@\\^`{|}&")
incomeType = incomeType.replacingOccurrences(of: "&", with: "%26")
incomeType = incomeType.replacingOccurrences(of: "'", with: "%27")
incomeName = incomeName.replacingOccurrences(of: "&", with: "%26")
incomeName = incomeName.replacingOccurrences(of: "'", with: "%27")
searchForIDServer.name = incomeName.addingPercentEncoding(withAllowedCharacters: characterSet)!
searchForIDServer.type = incomeType.addingPercentEncoding(withAllowedCharacters: characterSet)!
searchForIDServer.delegate = self
searchForIDServer.downloadItems()
} else {
searchForIDServer.name = incomeName.addingPercentEncoding(withAllowedCharacters: characterSet)!
itemPreBasketServer.id = String(incomeID)
itemPreBasketServer.delegate = self
itemPreBasketServer.downloadItems()
}
}
我还有一个名为cellItems的数组,它由searchForIDServer.downloaditems()或itemPreBaskeyServer.downloaditems()中的协议填充:
var cellItems: NSArray = NSArray() {
didSet {
tableView.reloadData()
}
}
func itemsDownloaded(items: NSArray) {
cellItems = items
tableView.reloadData()
}
现在问题是,在我的cellForRowAt函数中,当我在cellItems上创建一个var时,我得到一个错误:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item: ItemPreBasketModel = cellItems[indexPath.row] as! ItemPreBasketModel
if indexPath.section == 0 && indexPath.row == 0 {
//This will be the desc
let descCell: PreDescTableViewCell = tableView.dequeueReusableCell(withIdentifier: "descCell", for: indexPath)as! PreDescTableViewCell
print(item.name)
return descCell
} else if indexPath.section == 0 && indexPath.row == 1 {
//This will be the price
let priceCell: PrePriceTableViewCell = tableView.dequeueReusableCell(withIdentifier: "priceCell", for: indexPath)as! PrePriceTableViewCell
return priceCell
} else if indexPath.section == 0 && indexPath.row == 2 {
//This will be the quantity
let quantityCell: PreQuantityTableViewCell = tableView.dequeueReusableCell(withIdentifier: "quantityCell", for: indexPath)as! PreQuantityTableViewCell
return quantityCell
} else {
//Submit
let submitCell: PreSubmitTableViewCell = tableView.dequeueReusableCell(withIdentifier: "submitCell", for: indexPath)as! PreSubmitTableViewCell
return submitCell
}
}
我在运行时得到的错误是:
2018-01-31 16:41:28.884725+0000 SpiceCentral[5487:468249] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102a0912b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000101b50f41 objc_exception_throw + 48
2 CoreFoundation 0x0000000102a21ebd -[__NSArray0 objectAtIndex:] + 93
3 SpiceCentral 0x00000001011ba494 _T012SpiceCentral32ItemPreBasketTableViewControllerC05tableG0So07UITableG4CellCSo0jG0C_10Foundation9IndexPathV12cellForRowAttF + 132
4 SpiceCentral 0x00000001011bb0dc _T012SpiceCentral32ItemPreBasketTableViewControllerC05tableG0So07UITableG4CellCSo0jG0C_10Foundation9IndexPathV12cellForRowAttFTo + 92
5 UIKit 0x0000000103521484 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 778
6 UIKit 0x0000000103521a2a -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
7 UIKit 0x00000001034e71f6 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3031
8 UIKit 0x00000001035092e6 -[UITableView layoutSubviews] + 176
9 UIKit 0x0000000103491a6d -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1439
10 QuartzCore 0x000000010c45f61c -[CALayer layoutSublayers] + 159
11 QuartzCore 0x000000010c4637ad _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 401
12 QuartzCore 0x000000010c3ea86c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 364
13 QuartzCore 0x000000010c417946 _ZN2CA11Transaction6commitEv + 500
14 UIKit 0x00000001033ea167 _afterCACommitHandler + 272
15 CoreFoundation 0x00000001029abc07 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
16 CoreFoundation 0x00000001029abb5e __CFRunLoopDoObservers + 430
17 CoreFoundation 0x0000000102990124 __CFRunLoopRun + 1572
18 CoreFoundation 0x000000010298f889 CFRunLoopRunSpecific + 409
19 GraphicsServices 0x0000000108e869c6 GSEventRunModal + 62
20 UIKit 0x00000001033c05d6 UIApplicationMain + 159
21 SpiceCentral 0x00000001011c1ec7 main + 55
22 libdyld.dylib 0x000000010749bd81 start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我已经尽力解决这个问题,但没有运气。我在其他视图控制器中以完全相同的方式填充其他tableviews,唯一不同的是我预先将我的单元格数定义为4,并且我在ViewDidLoad中也使用了一些逻辑。我基本上是在说“嘿viewController,如果你还没有ID,请通过运行这个函数来获取它,这将填充我的cellItems,否则如果你有ID,运行这个函数。
答案 0 :(得分:1)
你是什么意思&#34;预先定义我的细胞计数为4&#34;?如果您定义以下方法:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
然后它看起来合乎逻辑,你得到一个错误。最开始cellItems
数组为空,但cellForRowAt
方法将被调用,因为numberOfRowsInSection
为4. cellItems[indexPath.row]
将引发错误,因为数组为空
答案 1 :(得分:0)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->
Int {
if( cellItems?.count < 4 ) return 0
return 4
/* Note: If cellItems will always have 4 items when it's loaded, the following line would be appropriate on its own: */
// return cellItems?.count
}