使用按钮将行添加到表视图
Adding rows to tableview with a button
我想制作一个在上角带有按钮的tableview。我希望按钮向表视图中再添加一行,以便您可以向单元格或文本中添加图像。
我在互联网上搜索过,但没有找到答案。
这里是源代码:
import UIKit
class TableViewController: UITableViewController {
var imageArray = ["1","2","3","4","5","6","7"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Reccept") as UITableViewCell!
let imageView = cell.viewWithTag(2) as! UIImageView!
imageView.image = UIImage(named: imageArray[indexPath.row])
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageArray.count
}
}
试试这个,
- 将您的 imageArray 更改为可以 append/update 的 mutable 数组 cos
你的数组
- 在按钮操作中更新 imageArray 中的图像
- 在按钮操作中重新加载您的 table 视图
EX:
var imageArray:NSMutableArray=NSMutableArray()
@IBAction func buttonaction(sender:AnyObject)
{
imageArray.addObject("Your image data")
your_tableView.reloadData()
}
而不是重新加载 table 视图,您应该使用 beginUpdates()
和 endUpdates()
因为 tableview.It 会更好..
首先,单击按钮时 table 查看数组中的所有附加数据
Yourarray.append("image data")
然后更新您的 table 并插入新行
// Update Table Data
tblname.beginUpdates()
tblname.insertRowsAtIndexPaths([
NSIndexPath(forRow: Yourarray.count-1, inSection: 0)
], withRowAnimation: .Automatic)
tblname.endUpdates()
extend array by inserting the upcoming value suppose you have
var array: [String] = ["Iphone4", "Iphone5", "Iphone6"]
//all the table functions here which are boiler plate//
then
var newValue = textbox.text
array.append = newValue
tablewview.reloadData()
我们可以使用 beginUpdates 和 endUpdates
而不是 Table 重新加载
为SWIFT3
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: yourArray.count-1, section: 0)], with: .automatic)
tableView.endUpdates()
对于 SWIFT 4 和 5
answerTable.beginUpdates()
answerTable.insertRows(at: [IndexPath(row:Yourarray.count-1, section: 0)], with: .automatic)
answerTable.endUpdates()
Swift5 岁及以上:
首先为 tableView 行的内容追加数组列表。
然后将这一行添加到按下按钮函数中:
tableView.insertRows(at: [IndexPath(row: array.count-1, section: 0)], with: .automatic)
我想制作一个在上角带有按钮的tableview。我希望按钮向表视图中再添加一行,以便您可以向单元格或文本中添加图像。 我在互联网上搜索过,但没有找到答案。
这里是源代码:
import UIKit
class TableViewController: UITableViewController {
var imageArray = ["1","2","3","4","5","6","7"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Reccept") as UITableViewCell!
let imageView = cell.viewWithTag(2) as! UIImageView!
imageView.image = UIImage(named: imageArray[indexPath.row])
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageArray.count
}
}
试试这个,
- 将您的 imageArray 更改为可以 append/update 的 mutable 数组 cos 你的数组
- 在按钮操作中更新 imageArray 中的图像
- 在按钮操作中重新加载您的 table 视图
EX:
var imageArray:NSMutableArray=NSMutableArray()
@IBAction func buttonaction(sender:AnyObject)
{
imageArray.addObject("Your image data")
your_tableView.reloadData()
}
而不是重新加载 table 视图,您应该使用 beginUpdates()
和 endUpdates()
因为 tableview.It 会更好..
首先,单击按钮时 table 查看数组中的所有附加数据
Yourarray.append("image data")
然后更新您的 table 并插入新行
// Update Table Data
tblname.beginUpdates()
tblname.insertRowsAtIndexPaths([
NSIndexPath(forRow: Yourarray.count-1, inSection: 0)
], withRowAnimation: .Automatic)
tblname.endUpdates()
extend array by inserting the upcoming value suppose you have
var array: [String] = ["Iphone4", "Iphone5", "Iphone6"]
//all the table functions here which are boiler plate//
then
var newValue = textbox.text
array.append = newValue
tablewview.reloadData()
我们可以使用 beginUpdates 和 endUpdates
而不是 Table 重新加载为SWIFT3
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: yourArray.count-1, section: 0)], with: .automatic)
tableView.endUpdates()
对于 SWIFT 4 和 5
answerTable.beginUpdates()
answerTable.insertRows(at: [IndexPath(row:Yourarray.count-1, section: 0)], with: .automatic)
answerTable.endUpdates()
Swift5 岁及以上:
首先为 tableView 行的内容追加数组列表。
然后将这一行添加到按下按钮函数中:
tableView.insertRows(at: [IndexPath(row: array.count-1, section: 0)], with: .automatic)