Ios 如何在Swift中将新单元格插入UITableView
我正在处理一个项目,其中我有两个Ios 如何在Swift中将新单元格插入UITableView,ios,iphone,swift,uitableview,Ios,Iphone,Swift,Uitableview,我正在处理一个项目,其中我有两个UITableViews和两个UITextFields,当用户按下按钮时,第一个textField中的数据应该进入tableView,第二个进入第二个tableView。我的问题是,我不知道如何将数据放入tableView每次用户按下按钮时,我都知道如何使用tableView:cellforrowatinexpath:插入数据,但据我所知,这只适用一次。那么,每次用户点击按钮时,我可以使用什么方法更新表格视图?使用开始更新和结束更新在点击按钮时插入新单元格 正如@
UITableView
s和两个UITextField
s,当用户按下按钮时,第一个textField
中的数据应该进入tableView
,第二个进入第二个tableView
。我的问题是,我不知道如何将数据放入tableView
每次用户按下按钮时,我都知道如何使用tableView:cellforrowatinexpath:
插入数据,但据我所知,这只适用一次。那么,每次用户点击按钮时,我可以使用什么方法更新表格视图
?使用开始更新
和结束更新
在点击按钮时插入新单元格
正如@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()
目标-C
[self.tblname beginUpdates];
NSArray *arr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:Yourarray.count-1 inSection:0]];
[self.tblname insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tblname endUpdates];
单击按钮时,使用
beginUpdates
和endUpdates
插入新单元格
正如@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()
目标-C
[self.tblname beginUpdates];
NSArray *arr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:Yourarray.count-1 inSection:0]];
[self.tblname insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tblname endUpdates];
以下是将数据添加到两个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中的代码:
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()
}
}
您的结果将是:
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()
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()
适用于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()
适用于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()
你试过什么?你说“tableView:cellForRowAtIndexPath:”就“你知道的”而言,“只起作用”。为什么不试试你按钮的方法?@Max von Hippel我所做的是:tableView从数组中获取数据,所以当我将一个项目附加到数组中时,我使用这个方法:tableView.reloadData这样当我附加一个项目时,数组将转到“CellForRowAtIndexPath”然后再次从数组中提取信息:)您尝试了什么?你说“tableView:cellForRowAtIndexPath:”就“你知道的”而言,“只起作用”。为什么不试试你按钮的方法?@Max von Hippel我所做的是:tableView从数组中获取数据,所以当我将一个项目附加到数组中时,我使用这个方法:tableView.reloadData这样当我附加一个项目时,数组将转到“CellForRowAtIndexPath”然后再次从数组中提取信息:)很高兴您也提供了Objective-C版本。有时候会有帮助@RashmiRanjanmallick…是的,和我想的一样,编辑我的答案…这样人们就能理解翻译:)
begin/endUpdates
对单个插入/删除/移动操作无效。@vadian-hmm。。。我就是这么想的。。。但是您所说的开始/结束更新对单个插入/删除/移动操作无效是什么意思???对不起,问题太多了。没有效果意味着行在那里与不在那里没有区别。您只需要这些行,例如,insert
行后跟delete
行或重复循环中同一行的多个调用。很高兴您也提供了Objective-C版本。有时候会有帮助@RashmiRanjanmallick…是的,和我想的一样,编辑我的答案…这样人们就能理解翻译:)begin/endUpdates
对单个插入/删除/移动操作无效。@vadian-hmm。。。我就是这么想的。。。但是您所说的开始/结束更新对单个插入/删除/移动操作无效是什么意思???对不起,问题太多了。没有效果意味着行在那里与不在那里没有区别。你只需要为一个insert
行加上一个delete
行或重复循环中同一行的多个调用使用这些行。故事板是如何安排的?我问的原因是我正在尝试做类似的事情,我在控制台中收到一条消息,告诉我tableview被强制执行这帮了大忙。谢谢。参考您的回答,我可以问一些与添加节和移动行相关的问题吗?故事板是如何布置的?我问的原因是我正在尝试做一些类似的事情,并且我在控制台中收到一条消息,告诉我tableview正在被迫重新调整大小。这非常有帮助。谢谢。参考您的答案,我可以问一些关于添加节和移动行的问题吗?集合视图是如何实现的?我可以轻松插入多个项目。。。。但这是在索引0处插入。选择项目时,会显示上一个索引0。如何使用集合视图实现这一点?我可以轻松插入多个项目。。。。但这是在索引0处插入。选择该项时,会显示上一个索引0。为什么要创建NSIndexPath,然后将其强制转换为IndexPath?最初只需创建一个索引XPath。我通常做得更简单:[0,1]
。零是节,1是行。为什么要创建NSIndex