bobdawg
bobdawg

Reputation: 69

Inserting items into an empty array

I am not completely sure if this is possible.. I am starting off with an empty array. I have a tableview each with different numbers. When the user clicks on a cell, the selected number is appended into the array.

Now, the problem I am facing is that the user can click on any cell in any order, but I need my array to be ordered so that cell 1 matches with index 1, cell 2 matches with index 2 and so on.

What I have tried so far is append. but this inserts numbers out of order. I have tried insert, but this throws an error after the first selection stating the index is out of range.

edit:

I have a tableView with numbers in numerical order: 1, 2 , 3 , 4 ... The user clicks on a number and that number is appended to the array.

The user can select any number in any order, for example: 1, 3 , 4 , 2 .. And I would end up with an array of [1,3,4,2]. I need the numbers to be ordered so that if the user were to select 3, it would end up in the third index of the array.

Any help is appreciated

Upvotes: 1

Views: 2202

Answers (6)

Eendje
Eendje

Reputation: 8883

I'm not exactly sure how you're going to use your array, but I wouldn't recommend using your own answer.

Instead of creating a fixed sized array you could use a Dictionary:

var dict: [Int: Int] = [:]

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    dict[indexPath.row +1] = indexPath.row + 1
}

Upvotes: 0

So Over It
So Over It

Reputation: 3698

I would agree with @bryanjclark.

I'm not sure that creating an array filled with 'nil' (as you suggest in your own answer) is the ideal solution.

Again, as @bryanjclark suggests, I would use Set.

var indexSet:Set<Int> = []

// click on index 1
indexSet.insert(1)
// click on index 5
indexSet.insert(5)

// get all clicked cell indexes back in order
let sortedIndexes = indexSet.sort()

// check if a index is in the clicked set
if indexSet.contains(3) {
    print("contains")
} else {
    print("does not contain")
}

Upvotes: 0

bobdawg
bobdawg

Reputation: 69

For anyone looking for the answer.. The code below will create an array with 4 nil placeholders.

var array = [Int?](count: 4, repeatedValue: nil)

to get the selected item at the correct index I used

array[indexPath.row] = tableView.cellForRowAtIndexPath(indexPath)!.textLabel!.text

Upvotes: 0

bryanjclark
bryanjclark

Reputation: 6404

Array's append and sort functions should work just fine for what you've described:

// Use `var`, not `let` - you're going to change this value over time!
var tappedNumbers: [Int] = []

// You can then append numbers in order, like this:
tappedNumbers.append(2)
tappedNumbers.append(5)
tappedNumbers.append(1)

tappedNumbers.sort() // this returns the array [1, 2, 5]


// If you don't want duplicates, try using Set instead of Array:
var tappedSet: Set<Int> = []
tappedSet.insert(2)
tappedSet.insert(5)
tappedSet.insert(1) 
tappedSet.insert(5) // see, we're double-inserting 5 here...

tappedSet.count // returns 3, because there are only 3 unique entries

tappedSet.sort() // returns [1, 2, 5]

Drop this code into a Swift Playground and try it yourself - is this what you're describing?

Upvotes: 1

brimstone
brimstone

Reputation: 3400

Assuming what you're asking for is a way to link the cell's index with the value of the cell, here's what you should do. Don't use the value of the cell's position in the array as what the cell index is, instead use a dictionary or array of tuples containing both the cell index and the cell value. Here's an example:

var myDict = Dictionary<Int,Int>()

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cellvalue = tableView.cellForRowAtIndexPath(indexPath)!.textLabel!.text
    myDict.append((index: indexPath.row, value: cell value))

} 

or with tuple:

typealias myTuple = (index: Int, value: Int)
var myTupleArray = Array<myTuple>()

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cellvalue = tableView.cellForRowAtIndexPath(indexPath)!.textLabel!.text
    myDict.append((index: indexPath.row, value: cell value))

} 

Upvotes: 0

Kyle Redfearn
Kyle Redfearn

Reputation: 2281

You could try starting out with an array of NSNull objects that is the same size as the number of rows in your table view. Then as the user selects the rows, replace the NSNull at that index with the selected number.

NSNull: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNull_Class/

Upvotes: 0

Related Questions