0

I am trying to build a tableview from some data that I have but I have come across some appending behaviour that I don't understand.

To debug I am trying this , if I say :

var sectionHeaders:[String] = ["0","1"]
var items:[[String]] = [["zero","zero","zero"],["one","one","one"]]

and display this in my tableview, I see a header "0" with three rows of zero, another header "1" with three rows of "one". Which is to be expected. However if I try and build the same structure using append, I get strange results :

var sectionHeaders:[String] = []
var items:[[String]] = [[]]
var tempItems:[String] = []

sectionHeaders.append("0")

        tempItems.append("zero")
        tempItems.append("zero")
        tempItems.append("zero")

        items.append(tempItems)

        tempItems = []

        sectionHeaders.append("1")

        tempItems.append("one")
        tempItems.append("one")
        tempItems.append("one")

        items.append(tempItems)

With this I get a section header of "0" with zero rows (no lines), and a header of "1" with three rows of zeros.

The data seems to be offset somehow

Is there something wrong with the way I am appending ?

My data source delegates are very simple :

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        return sectionHeaders[section]
    }
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return sectionHeaders.count
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return items[section].count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath)
        
        let text = items[indexPath.section][indexPath.row]
        
        cell.textLabel?.text = text
        
        return cell
    }

   
}
2
  • Instead of using arrays of arrays, you should use an array of Structs. Within each Struct you can an array. This makes it easier to keep track of. Commented Apr 5, 2021 at 20:18
  • Thanks, but how would a struct look for something like this ? I can't visualize it. Commented Apr 6, 2021 at 12:41

1 Answer 1

1

When you do this:

var items:[[String]] = [[]]

You're defining items as being an array (the outer set of brackets) with one element, which is an empty array: [] (the inner set of brackets).

To not get the offset, it should be this:

var items:[[String]] = []

(just an empty array)

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

1 Comment

Sure. When you are able, please mark the answer as correct by using the green checkmark.

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.