TableView Error

Hi, i am trying to follow this example: http://blog.anscamobile.com/2010/09/create-scrolling-list-views-with-text-and-graphics-in-coronasdk-ios-android-tutorial/ the first part is where im getting the problem:

My main.lua file looks like this:

[code]tableView = require(“tableView”)

local myList = tableView.newList{ }

local data = {
“Hot Coffee”,
“Iced Coffee”,
“Espresso”,
“Cappuccino”,
“Latte”,
“Americano”
}[/code]

and the error i seem to be getting is:

/Users//Desktop/List View App/tableView.lua:278: attempt to get length of local ‘data’ (a nil value)
stack traceback:
[C]: ?
/Users//Desktop/List View App/tableView.lua:278: in function 'newList’

the tableView.lua is within the same directory as my main.lua file…seems like such a easy tutorial but i cant even get past the first hurdle :frowning:

cheers [import]uid: 34863 topic_id: 22629 reply_id: 322629[/import]

Sounds like you may have a “scoping” problem with the “data” variable. Make sure you are not declaring “data” local twice. That would create two “data” variable and the second one would be nil. [import]uid: 7559 topic_id: 22629 reply_id: 90242[/import]

thanks for the quick reply, the variable data is only declared once in the whole of the main.lua file, and as you can see from the code it has been scoped locally, should still create a problem?.

also i have read elsewhere on the forum that using the widget library now the best way of creating a tableView rather than using the tableViewLibrary (like they do in the tutorial) [import]uid: 34863 topic_id: 22629 reply_id: 90246[/import]

Yeah, I can’t speak to how useful the old tableView is either, but if you look at the tutorial, it does two things:

  1. specifies data (tableView.newList{data=data}
  2. That means…you need to specify data before tableView. So in your first post the data table would need to come before myList.

[code]tableView = require(“tableView”)

local data = {
“Hot Coffee”,
“Iced Coffee”,
“Espresso”,
“Cappuccino”,
“Latte”,
“Americano”
}

local myList = tableView.newList{data=data,}[/code]

(Maybe it just looks for data{} by default, I’ve never looked into that. But you’d definitely need to declare the table before the function) [import]uid: 41884 topic_id: 22629 reply_id: 90248[/import]

The error message is telling you that “data” length is nil, which means it’s not the same “data” variable containing the Lua table. That means you are trying to use the “data” variable before it was defined or using it in another file and it’s no longer in scope. Doing the following where are trying to use the variable will display the value in the terminal/console:

print( "data = ", data )  

I’m betting it will display “nil”.

Using tableView in the widget library is the preferred way to create tables in Corona starting with build 704. [import]uid: 7559 topic_id: 22629 reply_id: 90252[/import]

@Tom ahh you were right Tom, i did get a nil in the terminal.

@Richard, i did what you said and worked perfectly!, thanks for both your help.

are there any limitations using the widget library over the tableView library? for example perhaps not being able to add icons in the widget library but you can in the tableView library?

i also red somewhere that you can’t do the Swipe thing in the widget library but you can in the tableView library ? [import]uid: 34863 topic_id: 22629 reply_id: 90254[/import]

Double posted, sorry [import]uid: 34863 topic_id: 22629 reply_id: 90255[/import]

I think swipe is part of the widget now but could be wrong. Generally speaking the advantage of tableview is that it’s just a chunk of code. You can rewrite it however you want. The widget is better setup for advanced work but you can’t rewrite it because the widget code is called from Corona itself.

I think based on current improvements I would personally use the widget, but for something basic tableview is pretty good. [import]uid: 41884 topic_id: 22629 reply_id: 90275[/import]