使用按钮向tableview添加行

时间:2015-10-03 19:02:33

标签: xcode swift xcode7

我想在上角创建一个带有按钮的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


    }


}

5 个答案:

答案 0 :(得分:8)

除了重新加载tableview之外,你应该使用beginUpdates()endUpdates()进行tableview.It会有更好的方法..

首先在按钮点击

上附加tableview数组中的数据
    Yourarray.append("image data")  

然后更新你的表并插入新行

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

答案 1 :(得分:1)

library(ggplot2)   # version 2.2.1 used
ggplot(df_ex, aes(x=ordered,y="",fill=clas)) + #x axis bias voltage dependence
  geom_tile() + 
  scale_fill_manual(values=c('Good'="green","Bad"="Blue","Ugly"="black"))+
  facet_wrap(~No,ncol=1,scales = "free_x")+
  theme(legend.position = "top",axis.text.y = element_text(size = 20,angle = 90),axis.text.x = element_text(size=12,face="bold",colour = "black"),
        axis.title.y = element_text(face="bold",size = 20, colour = "black"),
        axis.title.x = element_text(face="bold",size = 20 , colour = "black"),
        strip.text = element_text(size=26, face="bold"),
        strip.background = element_rect(fill="#FFFF66", colour="black", size=0.5),
        plot.title=element_text(face="bold",color="red",size=14),
        legend.title = element_text(colour="black", size=26,face="bold"),
        legend.text = element_text(colour="black", size=18))+
  labs(x = "address",y = "") + 
  scale_x_discrete(labels = setNames(df_ex$address, df_ex$ord)) +

答案 2 :(得分:0)

试试这个,

  1. 将imageArray更改为可追加/更新的可变数组cos 你的阵列
  2. 按钮操作
  3. 更新imageArray中的图片
  4. 按钮操作
  5. 重新加载表格视图

    EX:

    var imageArray:NSMutableArray=NSMutableArray()
    
    @IBAction func buttonaction(sender:AnyObject)
    {
      imageArray.addObject("Your image data")
      your_tableView.reloadData()
    } 
    

答案 3 :(得分:0)

我们可以使用beginUpdates和endUpdates代替表重新加载

对于SWIFT 3

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()

答案 4 :(得分:0)

对于Swift 5及更高版本:

首先在数组列表中附加tableView行的内容。

然后将此行添加到按钮功能:

tableView.insertRows(at: [IndexPath(row: array.count-1, section: 0)], with: .automatic)
相关问题