Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我想在swift3的tableview中插入行作为节_Ios_Uitableview_Swift3 - Fatal编程技术网

Ios 我想在swift3的tableview中插入行作为节

Ios 我想在swift3的tableview中插入行作为节,ios,uitableview,swift3,Ios,Uitableview,Swift3,我正在数组中成功地追加数据,但未插入和显示该数据 但是下面的错误显示 由于未捕获异常而终止应用程序 “NSInternalInconsistencyException”,原因:'尝试插入第4行 到节0中,但在 更新' 我试过这样做: super.viewDidLoad() array1=["A1","B1","C1","D1"] array2=["A2","B2","C2","D2"] array3=["A3","B3","C3","D3"] a

我正在数组中成功地追加数据,但未插入和显示该数据

但是下面的错误显示

由于未捕获异常而终止应用程序 “NSInternalInconsistencyException”,原因:'尝试插入第4行 到节0中,但在 更新'

我试过这样做:

        super.viewDidLoad()

    array1=["A1","B1","C1","D1"]
    array2=["A2","B2","C2","D2"]
    array3=["A3","B3","C3","D3"]
    array4=["A4","B4","C4","D4"]
    dict1=[0:array1,1:array2,3:array3,4:array4,2:names]
    print( (dict1))
    print( (dict1[1]?.count)!)
    print(dict1.keys.count)

}
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

{
   return (((dict1)[section])!.count)
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    if tableView == tableExplore {

    cellMain = tableView.dequeueReusableCell(withIdentifier:"cellExp", for: indexPath) as! ExploreLocallyCell
    cellMain.textLabel?.textColor = UIColor(red: 194.0/255, green: 198.0/255, blue: 198.0/255, alpha: 1.0)
        cellMain.textLabel?.text=(dict1[indexPath.section])?[indexPath.row]
    }
    return cellMain

}
 public func numberOfSections(in tableView: UITableView) -> Int // Default is 1 if not implemented
{


return dict1.keys.count
}
  func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    return (names)[section]

}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
  self.insert(section:indexPath.section, row: indexPath.row)

}
func insert(section:Int,row:Int)
{
   print(dict1[section]!)
    // Update Table Data
    tableView.beginUpdates()
 dict1[section]!.append("BAlu")
    print(dict1[section]!)
    tableView.insertRows(at: [IndexPath(row:dict1[section]!.count-1, section: section)], with: .automatic)
    tableView.endUpdates()
    tableView.reloadData()

}
// No need of tableView.beginUpdates() and tableView.endUpdates() while you need to do one or two modification in UITableView 
// While you are inserting row in table programatically then no need of tableView.reloadData() because its has no meaning to do it. You should either insert or reload. It will give you same result. Diff is reload will take more performs time then insert row. 

func insert(section:Int,row:Int)
{
    print(dict1[section]!)
    // Update Table Data
    dict1[section]!.append("BAlu")
    print(dict1[section]!)
    tableView.insertRows(at: [IndexPath(row:dict1[section]!.count-1, section: section)], with: .automatic)
}

//Or
func insert(section:Int,row:Int)
{
    print(dict1[section]!)
    // Update Table Data
    dict1[section]!.append("BAlu")
    print(dict1[section]!)
    tableView.reloadData()
}