xcode 使用按钮将行添加到 tableview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32926347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding rows to tableview with a button
提问by David Sundstr?m
I want to make a tableview with a button in the upper corner. I want the button to add one more row to a tableview, so you can add a image to the cell or text. I have search over the internet but I have not found an answer.
我想在上角制作一个带有按钮的表格视图。我希望按钮向 tableview 再添加一行,以便您可以向单元格或文本添加图像。我在互联网上搜索过,但没有找到答案。
Here is the source code:
这是源代码:
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
}
}
回答by EI Captain v2.0
Instead of reload tableview, you should use beginUpdates()
and endUpdates()
for tableview.It would be much better approach..
而不是重新加载tableview,你应该使用beginUpdates()
and endUpdates()
for tableview。这将是更好的方法..
First of all append data in your tableview array on button click
首先在单击按钮时在 tableview 数组中附加数据
Yourarray.append("image data")
Then update your table and insert new row
然后更新您的表格并插入新行
// Update Table Data
tblname.beginUpdates()
tblname.insertRowsAtIndexPaths([
NSIndexPath(forRow: Yourarray.count-1, inSection: 0)
], withRowAnimation: .Automatic)
tblname.endUpdates()
回答by umair
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()
回答by Manikandan D
Try this,
尝试这个,
- Change your imageArray as mutable array cos that can append/update your array
- Update your image in your imageArray at button action
- Reload your table view at the button action
- 将您的 imageArray 更改为可以追加/更新您的数组的可变数组 cos
- 在按钮操作中更新 imageArray 中的图像
- 在按钮操作中重新加载您的表格视图
EX:
前任:
var imageArray:NSMutableArray=NSMutableArray()
@IBAction func buttonaction(sender:AnyObject)
{
imageArray.addObject("Your image data")
your_tableView.reloadData()
}