在最后一节插入带有按钮的行表视图

Insert row tableview with a button at last section

我想在该部分制作一个带有按钮的表格视图。我希望按钮像这样向 tableview 添加一行

这里是源代码:

 func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int {
    if sectionInd == 0 {
    return others.count
        } else {
    return 1
        }
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {

    let cell = tableView.dequeueReusableCell(withIdentifier: "ShareCell", for: indexPath as IndexPath) as! SelectOthersTableViewCell
    cell.firstName.text = others[indexPath.row].firstname
    cell.lastName.text = others[indexPath.row].lastname
    return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "addCell", for: indexPath as IndexPath) as! addTableCell
            cell.addCells.tag = indexPath.row
            cell.addCells.addTarget(self, action: #selector(OthersViewController.addButtonClicked(sender:)), for: UIControlEvents.touchUpInside)
        return cell
    }
}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var height:CGFloat = CGFloat()
   if indexPath.section == 0 {
        height = 145
   } else {
        height = 50
    }
    return height
}

@objc func addButtonClicked(sender:UIButton) {
    data.append("Guest 1")
    let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)
    let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
    print("indexPath \(indexPath!)")
    selectedIndexes[indexPath!] = !(selectedIndexes[indexPath!] ?? false)
    tableView.reloadRows(at: [indexPath!], with: .automatic)
    tableView.beginUpdates()
    tableView.insertRows(at: [IndexPath(row: data.count-1, section: 0)], with: .automatic)
    tableView.endUpdates()
}

我需要帮助。如何通过点击图标 (+) 上的按钮添加新行?

fun onPlusButtonClicked(){
    sections.append(whatever you want)
    items[2].append(["1", "2", "3", "4"]) // whatever you want to add here
    tableview.reloadData() // you can call this on a background thread as well, if its not working
}

// 如何使用 tableview 的例子

var sections = Your array

var items = your array

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sections[section]
}

func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items[section].count
}

单击 "Add" 按钮时,您 不应 重新加载整个 table 视图,因为这会增加处理时间。而不是你可以使用 beginUpdatesendUpdates 用于在单击按钮时插入新单元格。

基本方法:

(1)。单击 "Add",更新 table-view 的数据源。

dataSource.append(NewRecord)

(2)。插入新单元格:

 tableView.beginUpdates()
 tableView.insertRows(at: [IndexPath(row: dataSource.count-1, section: 0)], with: .automatic)
 tableView.endUpdates()

审查您的代码:

func addButtonClicked(sender:UIButton) {
  data.append("Guest 1")
  .....
} 

您的数据源是 others,table 视图是在其上创建和配置的。 但是单击添加按钮(addButtonClicked 函数)时,您不会更新 others 数据源。请验证它,除了你的代码看起来不错。