iOS Question B4XTable with autosizing columns

Graeme Mitchell

Member
Licensed User
I am looking to have a B4Xtable with columns that autosize depending on the data in the column. I know there was an code snippet

https://www.b4x.com/android/forum/t...-columns-based-on-content.102678/#post-689169

but I was getting an error on the line

B4X:
Dim pnl As B4XView = Column.CellsLayouts.Get(i)

[<B4IArray 0x281c5fc90> valueForUndefinedKey:]: this class is not key value coding-compliant for the key CellsLayouts.

My question is, do i need a panel in the Designer called pnl?
 

Graeme Mitchell

Member
Licensed User
So the following;
B4X:
    For x = 0 To strColLen.Length -1
       NameColumn(x) =B4XTable1.AddColumn(strColName(x),B4XTable1.COLUMN_TYPE_TEXT)
       Log(GetType(NameColumn(x)))
   Next

gave me 5 log entries for _b4xtablecolumn
 
Upvote 0

Graeme Mitchell

Member
Licensed User
B4X:
'Code module
#Region  Project Attributes
   #ApplicationLabel: Scada Admin
   #Version: 1.0.0
   'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
   #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
   #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
   #Target: iPhone, iPad
   #ATSEnabled: True
   #MinVersion: 8

#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
   Dim listpart As List
'   
   Dim MyArray() As String
   Dim su As StringUtils
   Public CurrentRow As Int
   Private PickerFile As Picker
   Private lblFile As Label
   Private lblColNames As Label
   Private lblColumnChar As Label
   Private lblColWidth As Label
   Private lblFileLen As Label
   Private B4XTable1 As B4XTable
   Private NameColumn() As B4XTableColumn
   Private cvs As B4XCanvas
   Private xui As XUI
   
End Sub


Private Sub Application_Start (Nav As NavigationController)
   'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
   NavControl = Nav
   Page1.Initialize("Administration")
   Page1.Title = "Administration"
   Page1.RootPanel.Color = Colors.White
   Page1.RootPanel.LoadLayout("FileSelector")
   NavControl.ShowPage(Page1)
   Dim Job1 As HttpJob
   Job1.Initialize("NewFileList",Me)
   Job1.Download("http://192.168.100.5/scada/scada1/newfilelist.txt")
   
   PickerFile.SizeToFit
   Dim p As B4XView = xui.CreatePanel("")
   p.SetLayoutAnimated(0, 0, 0, 1dip, 1dip)
   cvs.Initialize(p)
End Sub

Sub PickerFile_ItemSelected (Column As Int, Row As Int)
   CurrentRow = 1
   
   B4XTable1.Clear
   
   FindRow(Row)

End Sub

Sub FillArray
   Dim listpart As List
   Dim Dummy() As String
   Dim Count As Int =6
   Dim partsList As List
   
       partsList = su.LoadCSV(File.DirDocuments, "NewFileList.txt", TAB)
       
       
       Dim MyArray (partsList.size-1) As String
       
       
       listpart.Initialize
           
       For i=6 To partsList.Size -2 'Step 6
           Dummy = partsList.Get (i)
       
       MyArray (i-6)= Dummy(0)
                   
       If Count = 6  Then
       listpart.Add(MyArray(i-6))
       Count = 0
       
       End If
           
       
       Count = Count +1
       Next
       
       PickerFile.SetItems(0,listpart)
       PickerFile.SetRowsHeight(40)
   
End Sub

Sub   FindRow(ThisRow As Int)
   CurrentRow = ThisRow
   FillForm
End Sub
Sub FillForm
   
   lblFile.Text = MyArray((CurrentRow *6)+1)
   lblFileLen.Text =  MyArray((CurrentRow *6)+2)
   lblColumnChar.Text =  MyArray((CurrentRow *6)+3)
   lblColNames.Text =  MyArray((CurrentRow *6)+4)
   lblColWidth.Text =  MyArray((CurrentRow *6)+5)
       
   Dim strColLen() As String=Regex.Split(",",lblColumnChar.Text)
   Dim strColName() As String=   Regex.Split(",",lblColNames.Text)
   Dim strColWidthChar() As String = Regex.Split(",",lblColWidth.Text)

   Dim filename As String
   
   filename = lblFile.Text.Replace("\\192.168.100.251\","http://192.168.100.5/")
   filename = filename.Replace("\","/")
   
   Dim textstart As Int = 0
   Dim textfinish As Int = 0
   Private NameColumn(strColLen.Length) As B4XTableColumn

   For x = 0 To strColLen.Length -1
   '   B4XTable1.AddColumn(strColName(x),B4XTable1.COLUMN_TYPE_TEXT)
       NameColumn(x) =B4XTable1.AddColumn(strColName(x),B4XTable1.COLUMN_TYPE_TEXT)
       NameColumn(x).Width = strColWidthChar(x) / 15
       Log(GetType(NameColumn(x)))
   Next
   
   NameColumn(0).InternalSortMode= "ASC"
   Dim data As List
   data.Initialize
   Dim LineArray(strColWidthChar.Length) As String
   Dim ColCount As Int = 0
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download(filename)
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
       For Each p As String In Regex.Split(CRLF, j.GetString)
           If p.Length > 0 Then
               For Each i As Int In Regex.Split(",",lblColumnChar.Text)
                   textstart =  textfinish
                   textfinish = textstart + i
                   LineArray(ColCount) = (p.SubString2(textstart, textfinish))
               '   Log(p.SubString2(textstart, textfinish))
                   ColCount = ColCount +1       
                   
               Next
           data.Add(LineArray)
               
           End If
           ColCount = 0
           textstart=0
           textfinish=0
           Dim LineArray(strColWidthChar.Length) As String
       Next
   End If
   B4XTable1.SetData(data)
   j.Release
   
   
End Sub

Sub B4XTable1_DataUpdated
   'Dim ShouldRefresh As Boolean

   'NameColumn and NumberColumn are global B4XTableColumns that we want to measure
'   For Each Column As B4XTableColumn In Array(NameColumn)
   '   Dim MaxWidth As Int
'       For i = 0 To B4XTable1.VisibleRowIds.Size
'           Dim pnl As B4XView = Column.CellsLayouts.Get(i)
'           Dim lbl As B4XView = pnl.GetView(0)
'           MaxWidth = Max(MaxWidth, cvs.MeasureText(lbl.Text, lbl.Font).Width + 10dip)
'       Next
'       If MaxWidth > Column.ComputedWidth Or MaxWidth < Column.ComputedWidth - 20dip Then
'           Column.Width = MaxWidth
'           ShouldRefresh = True
'       End If
'   Next
'   If ShouldRefresh Then
'       B4XTable1.Refresh
'   End If
End Sub

Sub Page1_KeyboardStateChanged (Height As Float)
   If B4XTable1.IsInitialized Then
       Dim base As Panel = B4XTable1.mBase
       Dim TableViewMaxHeight As Int = base.CalcRelativeKeyboardHeight(Height)
       Dim TableViewMaxHeightWithNoKeyboard As Int = Page1.RootPanel.Height - 2dip - B4XTable1.mBase.Top
       'If the keyboard is visible then the TableView ends at the keyboard's top line.
       'Otherwise the TableView will end a few pixels from the full page size.
       base.Height = Min(TableViewMaxHeight, TableViewMaxHeightWithNoKeyboard)
       PickerFile.Width = 100%x
   End If
End Sub

Sub JobDone (Job As HttpJob)
   If Job.Success = True Then
       Select Job.JobName
           Case "NewFileList"
               Dim out As OutputStream = File.OpenOutput(File.DirDocuments, "NewFileList.txt", False)
               File.Copy2(Job.GetInputStream, out)
               out.Close
               Log (Job.JobName & " Was Download")
           Case Else
               Log("Some Other Job Downloaded")
       End Select
   Else
       Log("Download Failed")
   End If
   Job.Release
   FillArray
End Sub
 
Upvote 0
Top