tableView and data numbering system

Is there a way to automate the data numbering within a tableView ?  

I currently have some 50 items  and I keep adding new stuff regularly. 

They don’t always come in neat order. So I find myself editing/ updating numberings manually very often. 

 

Instead of manually typing like this; 

 

Data[1] = AAA

Data[2] = BBB

Data[3] = CCC

Data[4] = DDD

 

Can I do something clever like this? ; 

 

Data[1] = AAA

Data[Previous number +1] = BBB

Data[Previous number +1] = CCC

Data[Previous number +1] = DDD

 

Any suggestion, advice would be much appreciated!

-Leo 

I don’t think tables will like arithmetic functions in the key. Try this :

local currentKey = 1

Data[currentKey] = AAA

 

currentKey + 1

Data[currentKey] = BBB

 

etc

on second thought… this activity is probably best served with a nice function. See following tutorial if you need more info on functions : http://coronalabs.com/blog/2013/10/08/tutorial-passing-data-tofrom-functions/

You could write it somewhat like the following I suppose : 

local Data = {} local function addToData(newData) local key = #Data + 1 Data[key] = newData return key end --usage local keyReturned keyReturned = addToData("AAA") -- use this method if you care about knowing the key assigned to AAA addToData("BBB") -- use this method if you don't care about the key for now' addToData("CCC") addToData("DDD")

Hope this helps.

If your data is numerically indexed, you an do:

local data = {}

data[#data+1] = someValue

to auto append your data to the end of the array.  If each data item itself is a table, it might look like this:

local data = {}

data[#data+1] = {}

data[#data].name = “Fred”

data[#data].id = #data

data[#data].someValue = someValue

data[#data+1] = {}

data[#data].name = “Barney”

data[#data].id = #data

data[#data].someValue = someValue

etc.

Rob

Thank you very much for your advices Ksan and Rob. 

I’ve fiddled round a bit and ended up using Rob’s method. It worked perfectly. 

So thank you both! 

I don’t think tables will like arithmetic functions in the key. Try this :

local currentKey = 1

Data[currentKey] = AAA

 

currentKey + 1

Data[currentKey] = BBB

 

etc

on second thought… this activity is probably best served with a nice function. See following tutorial if you need more info on functions : http://coronalabs.com/blog/2013/10/08/tutorial-passing-data-tofrom-functions/

You could write it somewhat like the following I suppose : 

local Data = {} local function addToData(newData) local key = #Data + 1 Data[key] = newData return key end --usage local keyReturned keyReturned = addToData("AAA") -- use this method if you care about knowing the key assigned to AAA addToData("BBB") -- use this method if you don't care about the key for now' addToData("CCC") addToData("DDD")

Hope this helps.

If your data is numerically indexed, you an do:

local data = {}

data[#data+1] = someValue

to auto append your data to the end of the array.  If each data item itself is a table, it might look like this:

local data = {}

data[#data+1] = {}

data[#data].name = “Fred”

data[#data].id = #data

data[#data].someValue = someValue

data[#data+1] = {}

data[#data].name = “Barney”

data[#data].id = #data

data[#data].someValue = someValue

etc.

Rob

Thank you very much for your advices Ksan and Rob. 

I’ve fiddled round a bit and ended up using Rob’s method. It worked perfectly. 

So thank you both!