xcodeuitableviewibaction

Populate TableView at button push in xCode


I have created a program with several screens that display sets of random numbers; it all works perfectly fine while populating the labels.

For one of the screens I need to show two independent results though. I tried this with Label and it didn't look all to well, so I thought about using TableView and now it is quite a challenge to actually push the result data to the rows of the table.

The code looks like this:

    class EController: UIViewController {

    @IBOutlet var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.dataSource = self
    
    }
    
    func allTogether(_ SetB:Set<Int>, _ Scope:ClosedRange<Int>) -> (Set<Int>){

        func iterateNremove2() -> (Set<Int>, Set<Int>){
        
            var nums = SetB
            let scp = Scope
        
            var reslts = Set<Int>()
        
            for _ in scp{
                let randNum = nums.randomElement()!
                reslts.insert(randNum)
                nums.remove(randNum)
            }
        
            return (nums, reslts)
        }

        let go = iterateNremove2()
        let res = go.1

        return (res)
    }
    
    @IBAction func Erand(_ sender: Any) {
        
        let it = allTogether(Set(1...45), 1...10)
        let that = allTogether(Set(1...9), 1...3)

        let eStrngM = it.map(String.init).joined(separator: ", ")
        let eStrngS = that.map(String.init).joined(separator: ", ")

        let finRslts = [eStrngM, eStrngS]
        
    }
    
    
}

extension EController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
        
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {
        let cell  = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        cell.textLabel?.text = finRslts[indexPath.row]
        
        return cell
    }
    
}

Sorry for such a big bulk, but the problem might be scattered in the code or the whole solution might be faulty (this is what I came up with after I watched the tutorials).

The only error I get is "Cannot find finRslts in scope" when "cell.textLabel?.text" is called.

I tried moving the randomizing function and the button's @IBAction between different classes and the error was always the same. I either miss something small and don't know how to search for it or the whole solution is faulty. Please help.


Solution

  • You have to create finRslts – by the way Swift doesn't have a 8+3 variable name restriction like in Windows 95 – on the top level of the class right after the table view outlet.

    var finalResults = [String]()
    

    In Erand replace

    let finRslts = [eStrngM, eStrngS]
    

    with

    finalResults = [eStrngM, eStrngS]
    tableView.reloadData()
    

    And it's good practice to return the number of items in the array in numberOfRowsInSection

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