【发布时间】:2018-10-25 04:44:46
【问题描述】:
我正在从 API 服务获取数据,我将这些数据传递给我的 tableview 并在其下创建部分和单元格。部分和单元的数量是动态的,取决于来自服务的数据。我的手机上有一个按钮。按钮名称为添加。当我单击添加按钮时,它会显示一个包含表格视图的警报。警报中的此表视图仅显示与其部分下的特定单元格相关的数据。我为按钮创建了一个委托方法,在该方法中我获取了该选定按钮的 indexPath.row 并将数据从我的模型传递到警报内的表视图。当我单击第一个单元格添加按钮时,它显示一切都很好,但是当我从第 2 部分中单击添加按钮时,它会兑现。我观察到该应用程序正在崩溃,因为编译器仅获取 indexPath.row 但它没有获取有关此单元格是哪个部分的信息。当按下添加按钮时,我如何才能知道我的委托函数选择了哪个部分单元格。这是我的单元类中委托函数的代码,
protocol ResMenuDetailDelegate {
func addOnBtnTapped(tappedIndex : Int)
}
var delegate: ResMenuDetailDelegate?
@IBAction func addBtnTapped(_ sender: Any) {
delegate?.addOnBtnTapped(tappedIndex: addBtn.tag)
}
在我的视图控制器类中,我符合委托方法,
extension RestaurantMenuDetailVC : ResMenuDetailDelegate{
func addOnBtnTapped(tappedIndex: Int) {
print(tappedIndex)
let addonCategory = subCategoryModel![tappedIndex].items[tappedIndex].addonCategory
print(addonCategory as Any)
}
这是我的 cellForRow 表视图委托,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == resMenuTableView{
let cell = resMenuTableView.dequeueReusableCell(withIdentifier: "detailMenuCell", for: indexPath) as! RestaurantMenuDetailTVC
cell.dishTitleLbl.text = subCategoryModel![indexPath.section].items[indexPath.row].itemName
cell.descriptionLbl.text = subCategoryModel![indexPath.section].items[indexPath.row].itemDescription
cell.priceLbl.text = String(subCategoryModel![indexPath.section].items[indexPath.row].itemPrice)
cell.addBtn.tag = indexPath.row
cell.delegate = self
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
return cell
}
【问题讨论】:
-
你会在 indexpath 中得到section和row,它是包含row和section的结构
-
但是我的崩溃与错误索引超出范围。 @RahulGUsai
标签: ios swift uitableview