【发布时间】:2017-12-01 07:11:33
【问题描述】:
我正在尝试使用具有 4 个部分的 UITableViewController(静态表格视图),每个部分都有不同类型的单元格。
部分中的单元格数量取决于从 Web 服务接收到的数据。在情节提要中,我在每个部分中创建了一种类型的单元格。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Number of rows in attachment sect
if section == 2 {
guard let currentDataModel = dataModel else { return 0 }
return currentDataModel.attachments.count
}
else if section == 3 {
guard let currentDataModel = dataModel else { return 0 }
return currentDataModel.contacts.count
}
return super.tableView(tableView, numberOfRowsInSection: section)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let currentSection = indexPath.section
if currentSection == 2 {
let currentRow = indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: "attachmentCell", for: indexPath) as! AttachmentViewCell
cell.setDataForCell(dataModel: dataModel?.attachments[currentRow], indexPath: indexPath)
return cell
}
//Show cells for Contact Section
else if currentSection == 3 {
let currentRow = indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactViewCell
cell.setDataForCell(dataModel: dataModel?.contacts[currentRow], indexPath: indexPath)
return cell
}
return super.tableView(tableView, cellForRowAt: indexPath)
}
我在调用 dequeueReusableCellWithIdentifier:forIndexPath: 时收到此错误:
由于未捕获的异常“NSRangeException”而终止应用,原因:“*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]”
我可以在 UITableViewController 的每个部分中使用不同类型的单元格和不同的行数吗?
【问题讨论】:
-
请分享一些尝试过的代码,如数组信息对于部分,以及 cellforRow 主代码
-
错误清楚地表明您正在访问大小为 2 的数组中的第三个元素。检查您在部分中的行数返回正确的行数
-
@Vinodh,是的,它返回该部分中正确的行数。
-
数组格式似乎是错误的,实际上并没有处理 Sections ,我使用了一个带有部分的 tableView,但如果你愿意,我可以与你分享所有部分的一个单元格,否则需要查看数组格式
-
崩溃发生在哪一行?
标签: ios swift uitableview