Using datasource and sqLite

I have a sqLite-database with about 500 entries. I want to populate an iOSTable using a datasource. What happens is that I get a table with the 500 elements, but only the first 8 entries from the database repeating.

This is what I did:

in Constructor(rcs as iOSSqLiteRecordSet)

redim tID(-1)
if rcs <> nil then
while not rcs.EOF
tID.append(rcs.Field(“ID”).IntegerValue)
rcs.movenext
wend
end if

in RowData
// Part of the iOSTableDataSource interface.

Dim c As iOSTableCellData = table.CreateCustomCell(GetTypeInfo(DishCustomCell))
Dim d as DishCustomCell = DishCustomCell(c.Control)

if GetDish(tID(row)) = true then
d.Name = Name
d.Ingredients = Ingredients
d.Url = Url
d.Icon = Icon
else
d.Name = tID(row).ToText
end if

return c

Thank you for a solution.

Hi @Herman Dubach

I can see that you store the ID field on an Array: an Integer number… and then, on the DataSource interface, you populate a custom cell from “Name”, “Ingredients”, “Url” and “Icon” variables? properties? Where are these stored and, most important, how do you update these values?

I mean, it doesn’t seems to me that these variables/properties get updated with different values for a particular row of the table cell.

Javier

Hi Javier,

In GetDish I read a record from the database with the key stored in tID().
GetDish puts the information in global properties.
If I set the rowHeight of the table to 60, I get 8 updated cells, if I set it to -1 I get 12 updated cells.
In other words, the property row never exceeds these values. Even if I scroll to the end of the table.
I assumed that row is updated by the system when you scroll, but this doesn’t happen.

Thanks for your answer,
Herman

Hi Herman,

Do you force the ReloadData of the table anywhere?

Javier

It doesn’t seem like attempting to pass values around using global properties is the best way to do this. Can you post your project (or a subset of it) so we can take a peek at what you are doing?

Hi Javier, Paul,

Sorry for not responding, I had to take care of my guests.

I don’t think the global properties has something to do with the problem. In a version where I access the database within RowData the same problem occurs. Tomorrow I will produce more info or post the project.

Thanks for now,
Herman

I think the problem is related to the CustomCell.

In RowData of the DataSource:

// This works fine, I get a table with all entries.

Dim cell As iOSTableCellData = table.CreateCell
if GetDish(tID(row)) = true then
cell.Text = Name
else
cell.text = tID(row).ToText
end if

return cell

// This doesn’t work but I can’t figure out why.

Dim c As iOSTableCellData = table.CreateCustomCell(GetTypeInfo(DishCustomCell))
Dim d as DishCustomCell = DishCustomCell(c.Control)

if GetDish(tID(row)) = true then
d.Name = row.totext + " " + Name
d.Ingredients = Ingredients
d.Url = Url
d.Icon = Icon
else
d.Name = tID(row).ToText
end if

return c

A few tips:

  1. When posting code here in the forum, be sure to wrap it in [code] tags (use the code button in the post toolbar) to make it easier for people to read.
  2. Keep in mind that when using CreateCustomCell, you do not get a new instance of the custom cell for each row. It’s possible that a previous instance will get re-used. So if you don’t re-populate all the values, they will retain the values from the re-used instance.
  3. Try setting some breakpoints or using the Break keyword so you can use the debugger to help you get a better feel for when your code is behaving unexpectedly.

If you can post a sample project, I can certainly take a peek at it.

Hi Paul,

Thanks for your attention.

I will send you TableDataSourceTest in which you can see my problem.
But how can I post this project, I never did that.

Any way you want. Dropbox, upload to your web site, OneDrive, etc, etc.

https://www.dropbox.com/s/ybz9ui7tn09i44s/TableDataSourceTest.xojo_binary_project?dl=0

thanks

You are using a Canvas to display the data, which is the wrinkle here. As I mentioned, you don’t necessarily get new instances of things when you create CustomCells.

You are correctly updating the Name property for your custom cell each time, but the issue is that if it is not a new instance, the Canvas will continue to display the old value. You need to tell the Canvas to update itself whenever you also update the Name property.

To get it to work, I just right-clicked on the Name property in the Navigator, selected “Convert to Computed Property” and then added this to the Set section:

Canvas1.Invalidate

I tried to follow your instructions but I don’t see the Convert to Computed Property option.
I deleted the existing Name-property and created a new Computed property Name.
Now Canvas1 can’t get the value of Name.

You can right-click on an existing property in the Navigator and select “Convert to Computed Property” from the menu. This creates a computed property that uses your existing property as the private backing value.

If you created your own computed property, then you’ll need to implement both the Get and Set section to have code to return the backing property and set its value. Or you could use a method of course.

You can learn about computed properties in the User Guide.

Thanks, it works.
I never used right click on my magic mouse. It was disabled.
Have a nice weekend.