使用按钮向tableview添加行

Dav*_*röm 0 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


    }


}
Run Code Online (Sandbox Code Playgroud)

EI *_*2.0 8

而不是重新加载tableview,你应该使用beginUpdates()endUpdates()for tableview.It会更好的方法..

首先,在按钮单击时在tableview数组中附加数据

    Yourarray.append("image data")  
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)