簡體   English   中英

在最后一節用按鈕插入行tableview

[英]Insert row tableview with a button at last section

我想在此部分中創建一個帶有按鈕的表格視圖。 我希望按鈕像這樣向表視圖中添加一行

在此處輸入圖片說明

這是源代碼:

 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()
}

我需要幫助。 如何通過點擊圖標(+)上的按鈕來添加新行?

單擊“添加”按鈕時, 不應重新加載整個表格視圖,因為這會增加處理時間。 取而代之的是,您可以使用beginUpdatesendUpdates在單擊按鈕時插入新單元格。

基本方法:

(1)。 單擊“添加”后,更新數據源以進行表視圖。

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")
  .....
} 

您的數據源是在其上創建和配置tableview的其他數據源。 但是在單擊添加按鈕( addButtonClicked函數)時,您沒有更新其他數據源。 請驗證它,除了您的代碼看起來不錯。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM