如何在Swift中将新单元格插入UITableView

cod*_*ons 67 iphone uitableview ios swift

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

EI *_*2.0 163

单击按钮时使用beginUpdatesendUpdates插入新单元格.

首先在tableview数组中追加数据

Yourarray.append([labeltext])  
Run Code Online (Sandbox Code Playgroud)

然后升级表并插入新行

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

这会插入单元格而不需要重新加载整个表,但是如果你遇到任何问题,你也可以使用 tableview.reloadData()


Swift 3.0

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

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];
Run Code Online (Sandbox Code Playgroud)

  • `begin/endUpdates`对单个插入/删除/移动操作没有影响. (7认同)
  • 很高兴你也提供了Objective-C版本.它有时帮助!! (3认同)
  • *没有效果*表示如果有线路没有差别.您需要的行仅用于例如`insert`行,后跟`delete`行或重复循环中同一行的多次调用. (3认同)

Dha*_*esh 22

以下是将数据添加到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()
    }
}
Run Code Online (Sandbox Code Playgroud)

你的结果将是:

在此输入图像描述


Sou*_*rma 21

Swift 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()
Run Code Online (Sandbox Code Playgroud)

在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()
Run Code Online (Sandbox Code Playgroud)