-1

I am new to this and need your help. Can anybody tell me how I can change this code so it produces an Array of Strings rather than an Array of Integers. Thank you for your time and any help you may have to offer.

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var objTable: UITableView!

    var numberArray = NSMutableArray()
    var selectedArray=NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        for index in 1...200 {
            numberArray.add(index)
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return numberArray.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell
    {
        let contact = numberArray.object(at: indexPath.row)
        let cell:MyCustomClass = objTable.dequeueReusableCell(withIdentifier: "reuseCell") as! MyCustomClass

        cell.textLabel?.text = String("Number \(contact)")

        cell.tickButton.addTarget(self, action:#selector(ViewController.tickClicked(_:)), for: .touchUpInside)

        cell.tickButton.tag=indexPath.row

        if selectedArray .contains(numberArray.object(at: indexPath.row)) {
            cell.tickButton.setBackgroundImage(UIImage(named:"Select.png"), for: UIControlState())
        }
        else
        {
            cell.tickButton.setBackgroundImage(UIImage(named:"Diselect.png"), for: UIControlState())
        }
        return cell
    }

    func tickClicked(_ sender: UIButton!)
    {
        let value = sender.tag;

        if selectedArray.contains(numberArray.object(at: value))
        {
            selectedArray.remove(numberArray.object(at: value))
        }
        else
        {
            selectedArray.add(numberArray.object(at: value))
        }

        print("Selected Array \(selectedArray)")

    objTable.reloadData()

    }

    func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) ->CGFloat
    {
        return 80.0
    }


}
Hamish
  • 78,605
  • 19
  • 187
  • 280

2 Answers2

1

Try it:

 let stringArray: [String] = (1...200).map {"Number " +  String(format: "%d", $0)}

Then remove this line:

let contact = numberArray.object(at: indexPath.row)

And change this line:

cell.textLabel?.text = String("Number \(contact)")

To:

cell.textLabel?.text = stringArray[indexPath.row]

Update:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var objTable: UITableView!

//    var numberArray = NSMutableArray()

    let stringArray: [String] = (1...200).map {"Number " +  String(format: "%d", $0)}

    var selectedArray=NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

//        for index in 1...200 {
//            numberArray.add(index)
//        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
//        return numberArray.count;
        return stringArray.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell
    {
//        let contact = numberArray.object(at: indexPath.row)
        let cell:MyCustomClass = objTable.dequeueReusableCell(withIdentifier: "reuseCell") as! MyCustomClass

//        cell.textLabel?.text = String("Number \(contact)")
        cell.textLabel?.text = stringArray[indexPath.row]

        cell.tickButton.addTarget(self, action:#selector(ViewController.tickClicked(_:)), for: .touchUpInside)

        cell.tickButton.tag = indexPath.row

        if selectedArray .contains(stringArray[indexPath.row]) {
            cell.tickButton.setBackgroundImage(UIImage(named:"Select.png"), for: UIControlState())
        }
        else
        {
            cell.tickButton.setBackgroundImage(UIImage(named:"Diselect.png"), for: UIControlState())
        }
        return cell
    }

    func tickClicked(_ sender: UIButton!)
    {
        let value = sender.tag;

        if selectedArray.contains(stringArray[value])
        {
            selectedArray.remove(stringArray[value])
        }
        else
        {
            selectedArray.add(stringArray[value])
        }

        print("Selected Array \(selectedArray)")

        objTable.reloadData()

    }

    func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) ->CGFloat
    {
        return 80.0
    }


}
javimuu
  • 1,829
  • 1
  • 18
  • 29
  • Thanks, but it didn't change the console display. Basically, what I'm trying to do is have the user checkmark items from a list and have the console print the user selected items in an array of strings. Each string in the array is the name of the item in the table view cell that that user selects. The code that I put above was the closest thing that I could find but it indexes integers (row numbers) instead of strings. If you want, you can download the xcode and run it, see what I am talking about -http://www.iosauthor.com/ios-tutorials/custom-tableview-checkboxes-using-swift/ – salanthonyc Mar 15 '17 at 05:49
  • You are welcome. If you have any problems, post it to stackoverflow and many people in there will help you. So [this site](https://www.raywenderlich.com/category/swift) has a lot of tutorials may you need. – javimuu Mar 15 '17 at 07:22
  • Hi javimuu. Is there a way to change the text in each cell? For example, instead of number 1, number 2, etc. , it could be car parts like ... door, tire, brake, headlight, etc.? And then the console would print those items instead of "number 1", etc. Anymore help would be great. Thanks. – salanthonyc Mar 15 '17 at 19:02
  • I also noticed that it only goes to the sixth cell instead of going to 80. Any suggestions? – salanthonyc Mar 15 '17 at 19:06
  • @salanthonyc just change this line `let stringArray: [String] = (1...200).map {"Number " + String(format: "%d", $0)}` to `let stringArray: [String] = ["door", "tire", "brake", headlight", "pen", "apple"] ` – javimuu Mar 16 '17 at 01:04
  • Okay, so i realize now that the return 80.0 at the bottom refers to the cell height because when I changed it to 10 the cells shrunk and it showed all of my items (twelve of them). However, when the cell height is 80 (which is a good size for the users, the simulator only allows six item (cells). Original code allowed for 200. Any advice? – salanthonyc Mar 18 '17 at 15:46
1

if you are not familiar with high order functions like the map function you can use a for loop like this...

var numberArray = [String]()

        for index in 1...200 {
            numberArray.append(String(index))
        }

The con of this approach is that numberArray is mutable because is a var, and you should avoid mutability in most cases.

This is the correct approach, and you can see that with this numberArray is a constant and we are not longer changing its state.

let numberArray = Array(1...200).map{String($0)}
James Rochabrun
  • 4,137
  • 2
  • 20
  • 17
  • Thanks. I'm very new to this and I am looking at Apple's Developer's Guide right now to try to understand your answer. If you have a chance, take a look at my comment to the first answer above to get a better understanding of what I am trying to accomplish. I you have anymore advice it will be appreciated. – salanthonyc Mar 15 '17 at 06:00