0

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.

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


    }


}
1
  • what did you try, where is the button's action ? Commented Oct 3, 2015 at 19:31

5 Answers 5

9

Instead of reload tableview, you should use beginUpdates() and endUpdates() for tableview.It would be much better approach..

First of all append data in your tableview array on button click

    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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, where is the "reload tableview"? And also "tableview.it". Thanks for all the help :)
You don't need to reload tableview because begin and end updates just reload one cell..so no need to reload whole table....:)
1
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()

Comments

0

Try this,

  1. Change your imageArray as mutable array cos that can append/update your array
  2. Update your image in your imageArray at button action
  3. Reload your table view at the button action

EX:

var imageArray:NSMutableArray=NSMutableArray()

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

4 Comments

If you have any doubt pls ref: stackoverflow.com/questions/32799098/…
you don't need the mutable array. you can append directly in swift
@William, Yes you are right. Thanks 'imageArray.append("your_image_data")'
Thanks for all the awnswr, really aprichiate it:)
0

Instead of Table reloading, We can use beginUpdates and endUpdates

For SWIFT 3

tableView.beginUpdates()
 tableView.insertRows(at: [IndexPath(row: yourArray.count-1, section: 0)], with: .automatic)
 tableView.endUpdates()

FOR SWIFT 4 and 5

 answerTable.beginUpdates()
 answerTable.insertRows(at: [IndexPath(row:Yourarray.count-1, section: 0)], with: .automatic)
        answerTable.endUpdates()

Comments

0

For Swift 5 and above:

First append the array list for content of tableView rows.

Then add this line to the press button function:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.