从表视图中删除单元格后更新indexPath.row

时间:2016-12-06 00:34:55

标签: ios swift uitableview

根据indexPath,我有一个表格视图,其中的单元格在点击时显示不同的表视图,即:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let nc = UINavigationController()
    let productController = ProductController()
    nc.viewControllers = [productController]

    //Apple
    if (indexPath.row == 0) {
        productController.navigationItem.title = "Apple Products";
    }
    //Google
    if (indexPath.row == 1) {
        productController.navigationItem.title = "Google Products";
    }
    //Twitter
    if (indexPath.row == 2) {
        productController.navigationItem.title = "Twitter Products";
    }
    //Tesla
    if (indexPath.row == 3) {
        productController.navigationItem.title = "Tesla Products";
    }
    //Samsung
    if (indexPath.row == 4) {
        productController.navigationItem.title = "Samsung Products";
    }
    present(nc, animated: true, completion: nil)
}

然而,当我删除这样的单元格时......

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
    if editingStyle == .delete
    {
        tableView.beginUpdates()
        CompanyController.companies.remove(at: indexPath.row)
        CompanyController.logos.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
        tableView.endUpdates()
    }
}

.... indexPath没有更新,所以如果我删除Apple单元格(在indexPath.row 0),Google单元格取而代之,但仍然会导致Apple产品页面,依此类推对其他公司而言。我认为tableView.delete行就是在处理它,但事实并非如此。删除某些内容后如何更新indexPath?

1 个答案:

答案 0 :(得分:2)

不要硬编码数据并假定特定的行。将数据放入数组中,并根据索引路径从数组中获取值。删除行时,通过从数组中删除来更新数据模型。

将您的php方法更新为:

didSelectRow
相关问题