iOS Question A Tableview example to add, del, edit, load and save

valentino s

Active Member
Licensed User
Longtime User
I'm learning how to use tableviews.

You can find attached a full working example to make all the ordinary things (add, edit, delete an item; load and save all the items in a text file).

I didn't found anything on the forum, so I've spent an afternoon to collect many infos from different sources. The code is also here to simplify those need a simple demo of it.

My question is: there are other better/faster methods to achive the results ?


I'm not yet confident in the relations between tableviews and lists. I often think I'm allocating twice the memory for the same datas. I load datas in a list, than I assign in a tableview. Is it a bad way to write code ? I now that tableview are "tipical" of small devices, to display items in an efficient way, but I guess if I consume the double of memory, for example.

Thanks to all in advance

B4X:
'Code module
#Region  Project Attributes
   #ApplicationLabel: demotableview
   #Version: 1.0.0
   'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
   #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
   #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page
   Public tv1 As TableView
   Private TextField1 As TextField
   Private Labelstatus As Label
   Private mylist As List
End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   mylist.Initialize
   Page1.RootPanel.LoadLayout("page1")
   NavControl.ShowPage(Page1)   
   'Labelstatus.Initialize("labelstatus")
   
   tv1.Initialize("tv1",False)
   Log ("top"&Labelstatus.Top)
   Page1.RootPanel.AddView(tv1,0,200,100%x,100%y-Labelstatus.Top)
   tv1.Clear
   tv1.AddSingleLine("Pippo1")
   tv1.AddSingleLine("Pippo2")
   tv1.AddSingleLine("Pippo3")
   tv1.AddSingleLine("Pippo4")
   tv1.AddSingleLine("Pippo5")
     
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
     tv1.SetLayoutAnimated(1500, 0.6, 0, 200 ,  100%x, 100%y-Labelstatus.top)
End Sub

Private Sub Application_Background
   
End Sub

Sub TV1_SelectedChanged (SectionIndex As Int, Cell As TableCell)
  Page1.Title = Cell.Text.ToString
  TextField1.Text=Cell.Text.ToString
  TextField1.Tag=Cell

End Sub

Sub Buttonadd_Click
   Dim items As List = tv1.GetItems(0)
   Dim tc As TableCell 'create a new item
   tc.InitializeSingleLine("New, edit it")
   items.InsertAt(0, tc)
   tv1.ReloadSection(0)
End Sub

Sub Buttondel_Click
   Dim cell = TextField1.tag As TableCell
   Dim items As List = tv1.GetItems(0)
   If (tv1.GetItems(0).IndexOf(cell)>-1) Then
     items.RemoveAt(tv1.GetItems(0).IndexOf(cell))
     tv1.ReloadSection(0)
   End If
End Sub

Sub Buttonedit_Click
   Dim cell = TextField1.tag As TableCell
   Dim s As AttributedString
   Log ("cell"&cell)
   If cell <> "" Then
    s.Initialize(TextField1.text, Font.DEFAULT, Colors.blue)
         cell.Text = s
'     mylist.cell.text=s
       cell.Update
   End If
End Sub

Sub Buttonload_Click
   If File.Exists(File.Dirdocuments,"listdemo.txt")=True Then
     tv1.Clear
     mylist=   File.ReadList(File.DirDocuments,"listdemo.txt")   
     Log("items found: "&mylist.Size)
     If mylist.size>1 Then
       For i= 0 To mylist.Size-1
         Log ("item "&i&mylist.Get(i))
         tv1.AddSingleLine(mylist.Get(i))
       Next
       tv1.ReloadAll
     End If
   Else
     Msgbox("No data previously saved","Error")
   End If
End Sub

Sub Buttonsave_Click
   Dim mylist As List
   mylist.Initialize
   For i=0 To tv1.GetItems(0).Size-1
     Dim mypippo As TableCell = tv1.GetItems(0).Get(i)
     Log ("saveitem "&i&mypippo.Text.ToString)
     mylist.Add (mypippo.text.ToString   )
   Next
   
   File.WriteList(File.DirDocuments,"listdemo.txt",mylist)
End Sub
 

Attachments

  • demotableview.zip
    3.2 KB · Views: 286
Top