【问题标题】:How to insert new cell into UITableView in Swift如何在 Swift 中将新单元格插入 UITableView
【发布时间】:2015-10-30 10:54:18
【问题描述】:

我正在做一个项目,我有两个UITableViews 和两个UITextFields,当用户按下按钮时,第一个textField 中的数据应该进入tableView,第二个进入进入第二个tableView。我的问题是,我不知道每次用户按下按钮时如何将数据放入tableView,我知道如何使用tableView:cellForRowAtIndexPath: 插入数据,但据我所知,这一次有效。那么每次用户点击按钮时我可以使用什么方法来更新tableView

【问题讨论】:

  • 你试过什么?你说“tableView:cellForRowAtIndexPath:”只能“据”你所知。为什么不在按钮的方法中尝试呢?
  • @Max von Hippel 我所做的是:tableView 从数组中获取数据,所以当我将一个项目附加到数组时,我使用这种方法:tableView.reloadData 每当我附加一个项目时数组将转到“CellForRowAtIndexPath”并再次从数组中提取信息:)

标签: ios iphone swift uitableview


【解决方案1】:

单击按钮时使用beginUpdatesendUpdates 插入一个新单元格。

正如@vadian 在评论中所说,begin/endUpdates 对单个插入/删除/移动操作无效

首先,在你的 tableview 数组中追加数据

Yourarray.append([labeltext])  

然后更新表格并插入新行

// Update Table Data
tblname.beginUpdates()
tblname.insertRowsAtIndexPaths([
NSIndexPath(forRow: Yourarray.count-1, inSection: 0)], withRowAnimation: .Automatic)
tblname.endUpdates()

这会插入单元格,不需要重新加载整个表格,但如果您对此有任何问题,您也可以使用tableview.reloadData()


Swift 3.0

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

Objective-C

[self.tblname beginUpdates];
NSArray *arr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:Yourarray.count-1 inSection:0]];
[self.tblname insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tblname endUpdates];

【讨论】:

  • 很高兴您也提供了 Objective-C 版本。有时会有所帮助!
  • @RashmiRanjanmallick ...是的,我想和编辑我的答案是一样的...所以人们理解翻译:)
  • begin/endUpdates 对单个插入/删除/移动操作无效。
  • @vadian 嗯...这就是我的想法...但是begin/endUpdates has no effect for a single insert/delete/move operation这句话是什么意思???抱歉问了太多问题..
  • No effect 表示有无线条没有区别。您只需要这些行,例如insert 行后跟 delete 行或重复循环中同一行的多次调用。
【解决方案2】:

Swift 5.0、4.0、3.0更新的解决方案

在底部插入

self.yourArray.append(msg)

self.tblView.beginUpdates()
self.tblView.insertRows(at: [IndexPath.init(row: self.yourArray.count-1, section: 0)], with: .automatic)
self.tblView.endUpdates()

在 TableView 顶部插入

self.yourArray.insert(msg, at: 0)
self.tblView.beginUpdates()
self.tblView.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .automatic)
self.tblView.endUpdates()

【讨论】:

  • 集合视图是如何实现的?我可以轻松插入多个项目....但这是在索引0中插入0.选择项目时,显示了先前的索引0。 span>
【解决方案3】:

这是您将数据添加到两个 tableView 的代码:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var table1Text: UITextField!
    @IBOutlet weak var table2Text: UITextField!
    @IBOutlet weak var table1: UITableView!
    @IBOutlet weak var table2: UITableView!

    var table1Data = ["a"]
    var table2Data = ["1"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func addData(sender: AnyObject) {

        //add your data into tables array from textField
        table1Data.append(table1Text.text)
        table2Data.append(table2Text.text)

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            //reload your tableView
            self.table1.reloadData()
            self.table2.reloadData()
        })


        table1Text.resignFirstResponder()
        table2Text.resignFirstResponder()
    }

    //delegate methods
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == table1 {
            return table1Data.count
        }else if tableView == table2 {
            return table2Data.count
        }
        return Int()
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if tableView == table1 {
            let cell = table1.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

            let row = indexPath.row
            cell.textLabel?.text = table1Data[row]

            return cell
        }else if tableView == table2 {

            let cell = table2.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! UITableViewCell

            let row = indexPath.row
            cell.textLabel?.text = table2Data[row]

            return cell
        }

        return UITableViewCell()
    }
}

你的结果将是:

【讨论】:

  • 故事板是如何布局的?
  • 我问的原因是我正在尝试做类似的事情,我在控制台中收到一条消息,告诉我 tableview 被强制调整大小
  • 这很有帮助。谢谢。
  • 参考你的回答,我可以问有关添加部分和移动行的问题吗?
【解决方案4】:

对于 Swift 5

删除单元格

    let indexPath = [NSIndexPath(row: yourArray-1, section: 0)]
    yourArray.remove(at: buttonTag)
    self.tableView.beginUpdates()

    self.tableView.deleteRows(at: indexPath as [IndexPath] , with: .fade)
    self.tableView.endUpdates()
    self.tableView.reloadData()// Not mendatory, But In my case its requires

添加新单元格

    yourArray.append(4)

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

【讨论】:

  • 为什么要创建一个 NSIndexPath 然后将其转换为 IndexPath?最初只需创建一个 IndexPath。我通常做得更简单:[0, 1]。 0 是节,1 是行。
【解决方案5】:

您可以在 Apple 的 UITableView 框架中找到此评论:

// Use -performBatchUpdates:completion: instead of these methods, which will be deprecated in a future release.

所以,你应该使用这个:

tableView.performBatchUpdates { [unowned self] in
    tableView.insertRows(at: indexPaths, with: animation)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多