Convert old application to use iOSTableDataSource

I try to convert an ‘old’ iOS example (using AddRow(section, value)) to an application based on the use of iOSTableDataSource. I have two sections. The section titles are displayed well but all names are added to both sections. The following code is used in RowData:

Var cell As MobileTableCellData = table.CreateCustomCell(GetTypeInfo(TableCustomCell))
Var customCell As TableCustomCell = TableCustomCell(cell.Control)
customCell.StudentName = Names(row)
Return cell```

But how can I add code into RowData to decide in which section the name must displayed? An easy example could be to store all names starting with the character 'A' in section 0 and all other names in section 1.

Doesnt the RowData event pass ‘section’ ?
Just wrap this up in

select case section

case 0
//zero section values

case 1 
//etc

end select

or , (based on this.)

An easy example could be to store all names starting with the character ‘A’ in section 0 and all other names in section 1.

if Names(row).left(1) = "A" and section = 0 then
Return cell
else
Return cell
end if

Thank you! This is my new code:

Select Case section
Case 0
  If Names(row).Left(1) = "A" Then
    Var cell As MobileTableCellData = table.CreateCustomCell(GetTypeInfo(TableCustomCell))
    Var customCell As TableCustomCell = TableCustomCell(cell.Control)
    customCell.StudentName = Names(row)
    Return cell
  End If
Case 1
  If Names(row).Left(1) <> "A" Then
    Var cell As MobileTableCellData = table.CreateCustomCell(GetTypeInfo(TableCustomCell))
    Var customCell As TableCustomCell = TableCustomCell(cell.Control)
    customCell.StudentName = Names(row)
    Return cell
  End If
End Select

Names starting with ‘A’ are in section 0 the other names are in section 1. That’s correct, but in both sections there are empty rows for the names placed in the other section. What am I doing wrong?


Case else
return nil

maybe?

Same problem: Empty rows…

What are you returning in the RowCount event?
RowCount return value is specific to each section.

When the If evaluates to False, you are skipping a cell so you end up with blank cells. You could create new arrays like NamesA() and NamesNotA() and use those here.

You can also move the Return cell to the bottom and not duplicate it.

I return the number of elements in Names!

Then you need to change that to return the number of elements starting with A on section 0 and not starting with A in section 1.

https://documentation.xojo.com/api/ios/iosmobiletabledatasource.html

Or you could follow this piece of advice

1 Like

@Jeremie_L Indeed, you have to return the number of elements in both sections. But only this change doesn’t solve the problem.
@Art_Gorski Indeed, you have to create arrays (or a dictionary) with the elements split up for both sections.

A combination of both replies solves the problem. Thank you for your help!

RowCount:

// Part of the iOSMobileTableDataSource interface.

Select case section
Case 0
  Return NamesA.Count
Case 1
  Return NamesNotA.Count
End Select

RowData:

// Part of the iOSMobileTableDataSource interface.

Select Case section
Case 0
  Var cell As MobileTableCellData = table.CreateCustomCell(GetTypeInfo(TableCustomCell))
  Var customCell As TableCustomCell = TableCustomCell(cell.Control)
  customCell.StudentName = NamesA(row)
  Return cell
Case 1
  Var cell As MobileTableCellData = table.CreateCustomCell(GetTypeInfo(TableCustomCell))
  Var customCell As TableCustomCell = TableCustomCell(cell.Control)
  customCell.StudentName = NamesNotA(row)
  Return cell
End Select