iOS Question A Rich String Formatting For The TableView

Discussion in 'iOS Questions' started by RichardN, Jun 8, 2015.

  1. RichardN

    RichardN Active Member Licensed User

    I am having great difficulty in utilising the RichString class to format a string to add to the TableView.

    The code appears simple enough but the class appears to return a (label) object where the TableView is expecting a string. I simply wish to pass a series of rich strings as a single line to the TableView formatted for UL & colour.

    What is the simplest way of coding this ?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You can get the AttributedString with RichString.AttributedString.
     
  3. RichardN

    RichardN Active Member Licensed User

    Thanks Erel. Sorry to be a pain but there is something fundamental I am missing here relating to the Richstring Class.

    Returning the RichString.AttributedString method from a subroutine is still giving a warning #7.... 'Object converted to string. This is probably a programming mistake'

    I have the RichString Class module loaded and all the methods are correctly exposed in the main module. The code looks something like this:


    Code:
    Sub Process_Globals
        
    Public App As Application
        
    Public NavControl As NavigationController
        
    Private Page1 As Page
        
    Private MyResultSet as SQL
        
    Private TableView1 As TableView
    End Sub

    Private Sub Application_Start (Nav As NavigationController)
        NavControl = Nav
        Page1.Initialize(
    "Page1")
        Page1.Title = 
    "My Table List"
        Page1.RootPanel.Color = 
    Colors.White

        MyResultSet = 
    SQL.ExecQuery("SELECT * FROM MyTable ORDER BY MyField")
        NavControl.NavigationBarVisible = 
    False
        TableView1.Initialize(
    "TableView1",False)
        Page1.RootPanel.AddView(TableView1, 
    00100%x100%y)
        
    Dim MyRecord as string

        
    Do While MyResultSet.NextRow
         
              MyRecord = MyResultSet.GetString(
    "MyField")
              TableView1.AddSingleLine(RsFormat(MyRecord))
     
         
    Loop
     
        NavControl.ShowPage(Page1)
     
    End Sub


    Sub RsFormat(subject As StringAs RichString

        
    Dim rs As RichString
        
    Dim l As Int = subject.Length -1
     
        rs.Initialize(subject)
        rs.BackgroundColor(
    Colors.Black , 0, l)
        rs.Color(
    Colors.Green , 0, l)
        rs.Underline(
    True,Colors.Green,0,l)

        
    Return rs.AttributedString                       'Returning wrong type of Object ?

    End Sub

    Where am I going wrong here ?
     
    Last edited: Jun 9, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    TableView.AddSingleLine expects a string. It will not work with AttributedString. Instead you should do:
    Code:
    Dim tc As TableCell = TableView1.AddSingleLine("")
    tc.Text = 
    'attributedstring here
     
  5. RichardN

    RichardN Active Member Licensed User

    Thanks Erel I get the difference now. Given that many of us are going back to school with B4i the difference between RichString and AttributedString needs to be understood.

    Having made that step I have encountered further limitations. I am trying to construct a two-line ListView TableView that has many major items each with a minor item below. On the face of it the stock TableView looks good using:

    Code:
    TableView1.AddTwoLines ("Major Item","Minor Item")
    Unfortunately for user consistency I need an overall black background with green text and partial underlining for the major line and plain white text for the minor item. Like this.....

    [​IMG]

    It seems the only way to achieve this flexibility is to create a single line TableView with CustomView Cells each with 2 labels inside a parent panel. The labels are made colors.black in different font sizes and WILL accept both plain text and AttributedText.

    Unfortunately when I run the program loading ~100 database strings... ***only the last loaded cell appears on the TableView***. Any idea why this might be ?

    Code:
    Do While Warnings.NextRow
          
    Dim tCell As TableCell = TableView1.AddSingleLine("")
          tCell.ShowSelection = 
    False
          tCell.CustomView = CreateItem(TableView1,Warnings.GetString(
    "Warning"), Warnings.GetString("System"))
        
    Loop


    Private Sub CreateItem(tvTableView As TableView, TopLine As String, BottomLine As StringAs Panel

        
    Dim lblWarning, lblSystem  As Label

        ParentPanel.Initialize(
    "ParentPanel")
        ParentPanel.Width=
    100%x
        ParentPanel.Height=tvTableView.RowHeight
       
        lblWarning.Initialize(
    "lblWarning")
        lblWarning.Font = 
    Font.CreateNew(20)
        lblWarning.color=
    Colors.Black
        lblWarning.TextColor=
    Colors.Green
       
        
    Dim rs As RichString
        rs.Initialize(TopLine)
        rs.Underline(
    True,Colors.green ,0,uLine(TopLine))          'Underline length varies with subject
        rs.SetToLabel(lblWarning)
       
        lblSystem.Initialize(
    "lblSystem")
        lblSystem.Font = 
    Font.CreateNew(16)
        lblSystem.Color = 
    Colors.Black
        lblSystem.TextColor = 
    Colors.White
        lblSystem.Text = BottomLine                                
    'No text formatting here
       
        
    Dim HalfHeight As Int = tvTableView.RowHeight/2
       
        ParentPanel.AddView(lblWarning,
    0,0,100%x,HalfHeight)
        ParentPanel.AddView(lblSystem,
    0,HalfHeight,100%x,HalfHeight)
       
        
    Return(ParentPanel)
       
    End Sub
     
  6. RichardN

    RichardN Active Member Licensed User

    To Answer my own question.....

    The panel 'ParentPanel' was defined in Process_Globals. When I made it a Local variable in the Sub 'CreateItem' it worked no problem.

    But I have absolutely no idea why!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice