vendredi 8 mai 2015

Swift Tableview Cells populated with name and a Int value

I'm trying to build my first app and i'm hitting a wall

I have a view controller and inside this view controller is a UITableView

Now I have been able to populate this view controller using an array.count

I have been able to add an accesory checkmark when i tap on the item to mark sa completed

Now my problem is that i want to tap on an item, mark it as completed AND when completed to have a value written on a label

each cell would have a different Int value

I've read somewhere that to do so i would need to use NSMutableArray but i don't know how to use this

any help would be really appreciated

heres the code :

UPDATED may 8 1215 utc

import UIKit

// --------------------------------------------------------------------------------------------------------------------------\
class ViewController: UIViewController, UITableViewDataSource {                        //class and subclass                  |)
//---------------------------------------------------------------------------------------------------------------------------/
    // Variable and constant, also IBAOutlet


    struct CellData
    {
        var title: String
        var value: String
        var selected: Bool

    }

    var CellData1 = CellData(title: "this is a test", value: "1", selected: true)
    var CellData2 = CellData(title: "this is a test", value: "1", selected: true)
    var CellData3 = CellData(title: "this is a test", value: "1", selected: true)
    var CellData4 = CellData(title: "this is a test", value: "1", selected: true)

    var tableArray: [CellData] = []

    @IBOutlet weak var scoreshow: UILabel!
    @IBOutlet weak var tableView: UITableView!

// --------------------------------------------------------------------------------------

    override func viewDidLoad() {

        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
    }
//----------------------------------------------------------------------------------------
    // checkmarks when tapped

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            if cell.accessoryType == .Checkmark
            {
                cell.accessoryType = .None
                cell.backgroundColor = UIColor.whiteColor()
            }
            else
            {
                cell.accessoryType = .Checkmark
                cell.backgroundColor = UIColor.yellowColor()            }
        }    
    }

//----------------------------------------------------------------------------------------
    //number of sections for the table

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 5
    }
//----------------------------------------------------------------------------------------
    //Calculate the amount of rows

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.tableArray.count;
    }
//----------------------------------------------------------------------------------------
    //Cells text label and config

    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{


        let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")
        cell.textLabel!.text = (tableArray[indexPath.row]).title
        scoreshow.text = (tableArray[indexPath.row]).value
        cell.textLabel!.numberOfLines = 0
        cell.selectionStyle = UITableViewCellSelectionStyle.None;

        return cell
 }

//----------------------------------------------------------------------------------------
    //resetbutton

    @IBAction func resetcheck(sender: UIButton) {

            for i in 0...tableView.numberOfSections()-1
            {
                for j in 0...tableView.numberOfRowsInSection(i)-1
                {
                    if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: j, inSection: i)) {
                        cell.accessoryType = .None
                        cell.backgroundColor = UIColor.whiteColor()
                    }

                }
            }
        }

//----------------------------------------------------------------------------------------

Aucun commentaire:

Enregistrer un commentaire