-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
    }


}
2
  • Please only post code relevant to your question which is basically just the contents of your viewDidLoad method. Commented Mar 15, 2017 at 3:06
  • 2
    let numberArray = Array(1...100).map{String($0)} Commented Mar 15, 2017 at 3:10

2 Answers 2

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
    }


}
Sign up to request clarification or add additional context in comments.

6 Comments

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 -iosauthor.com/ios-tutorials/…
You are welcome. If you have any problems, post it to stackoverflow and many people in there will help you. So this site has a lot of tutorials may you need.
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.
I also noticed that it only goes to the sixth cell instead of going to 80. Any suggestions?
@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"]
|
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)}

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.