iOS Question tableview showselection color

tufanv

Expert
Licensed User
Longtime User
Hello

Tableview's showselection makes the selected cell gray. Is there any way to change it. I ofund an objective code i dont know if it is related . The code is :

B4X:
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];

if this is true how can i implement it in b4i ?

Also for a tablecell i want to make a portion of the text bold . Is it possible ?

ty
 
Last edited:

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

Here:
B4X:
Sub SetSelectedColor(TV As TableView,Color As Int, Row As Int,Section As Int)
    Dim BgView As Panel
    BgView.Initialize("")
    BgView.Color = Color
   
    Dim NaObj As NativeObject
    NaObj = NaObj.Initialize("NSIndexPath").RunMethod("indexPathForRow:inSection:",Array(Row,Section))
   
    Dim NaObj2 As NativeObject = TV
    NaObj2.RunMethod("cellForRowAtIndexPath:",Array(NaObj)).RunMethod("setSelectedBackgroundView:",Array(BgView))
End Sub

This sub will only work, when the tableview is visible, so you can't call it for example in the Application_Start sub, but in the Page_Resize sub ....

Jan
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Jan,

Thank you very much ! ichartboost is also saving my date since you build it. I wanted to say this again =) .

Regards
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Hello Jan,

I could not completely made it work. I put the code:
B4X:
Sub TableView1_SelectedChanged (SectionIndex As Int, Cell As TableCell)
Page1.ResignFocus   
SetSelectedColor(TableView1,Colors.Red,0,SectionIndex)
End Sub

to try . For the row 1 this works good. When i select row1 the background goes red. When otheritems selected they go gray but when i scroll down to 8th or 9 th item it gives error:

Object was not initialized (NSObject)

for the line:
B4X:
    NaObj2.RunMethod("cellForRowAtIndexPath:",Array(NaObj)).RunMethod("setSelectedBackgroundView:",Array(BgView))

Am I using it wrong ? Do i have to add this code for every cell ( I use cells in my table btw ) or can i set a default color so everytime a cell is selected they go that color ?



Hi,

Here:
B4X:
Sub SetSelectedColor(TV As TableView,Color As Int, Row As Int,Section As Int)
    Dim BgView As Panel
    BgView.Initialize("")
    BgView.Color = Color
  
    Dim NaObj As NativeObject
    NaObj = NaObj.Initialize("NSIndexPath").RunMethod("indexPathForRow:inSection:",Array(Row,Section))
  
    Dim NaObj2 As NativeObject = TV
    NaObj2.RunMethod("cellForRowAtIndexPath:",Array(NaObj)).RunMethod("setSelectedBackgroundView:",Array(BgView))
End Sub

This sub will only work, when the tableview is visible, so you can't call it for example in the Application_Start sub, but in the Page_Resize sub ....

Jan
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

using the SelectedChanged event is a good idea, I have modify the code a bit. Now it should work better:

B4X:
Sub TableView1_SelectedChanged (SectionIndex As Int, Cell As TableCell)
   SetSelectedColor(Cell,Colors.Red)
End Sub


Sub SetSelectedColor(Cell As TableCell,Color As Int)
    Dim BgView As Panel
    BgView.Initialize("")
    BgView.Color = Color
   
    Dim NaObj2 As NativeObject = Cell
    NaObj2.GetField("nativeCell").RunMethod("setSelectedBackgroundView:",Array(BgView))
End Sub

Jan
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Now works perfect Jan !
Thank you very much !
Hi,

using the SelectedChanged event is a good idea, I have modify the code a bit. Now it should work better:

B4X:
Sub TableView1_SelectedChanged (SectionIndex As Int, Cell As TableCell)
   SetSelectedColor(Cell,Colors.Red)
End Sub


Sub SetSelectedColor(Cell As TableCell,Color As Int)
    Dim BgView As Panel
    BgView.Initialize("")
    BgView.Color = Color
  
    Dim NaObj2 As NativeObject = Cell
    NaObj2.GetField("nativeCell").RunMethod("setSelectedBackgroundView:",Array(BgView))
End Sub

Jan
 
Upvote 0

narek adonts

Well-Known Member
Licensed User
Longtime User
Hi,

using the SelectedChanged event is a good idea, I have modify the code a bit. Now it should work better:

B4X:
Sub TableView1_SelectedChanged (SectionIndex As Int, Cell As TableCell)
   SetSelectedColor(Cell,Colors.Red)
End Sub


Sub SetSelectedColor(Cell As TableCell,Color As Int)
    Dim BgView As Panel
    BgView.Initialize("")
    BgView.Color = Color
  
    Dim NaObj2 As NativeObject = Cell
    NaObj2.GetField("nativeCell").RunMethod("setSelectedBackgroundView:",Array(BgView))
End Sub

Jan
How did you find out that there is a method cell.nativeCell ?)))
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Upvote 0

tufanv

Expert
Licensed User
Longtime User
BTW, is there any way to make the first cell selected with this color ? I use
B4X:
TableView1.SetSelection(0,0)
to select the first cell automaticly on launch but with your vode i can only make the cell color as i like after a click made by the user ?

TY

Hi,

using the SelectedChanged event is a good idea, I have modify the code a bit. Now it should work better:

B4X:
Sub TableView1_SelectedChanged (SectionIndex As Int, Cell As TableCell)
   SetSelectedColor(Cell,Colors.Red)
End Sub


Sub SetSelectedColor(Cell As TableCell,Color As Int)
    Dim BgView As Panel
    BgView.Initialize("")
    BgView.Color = Color
  
    Dim NaObj2 As NativeObject = Cell
    NaObj2.GetField("nativeCell").RunMethod("setSelectedBackgroundView:",Array(BgView))
End Sub

Jan
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Yes ;)

Edit: It's better to use the following code to get the cell, so you need just the one sub:

B4X:
Dim TC As TableCell = TableView1.GetItems(0).Get(0)

'Now call the sub from #5
 
Last edited:
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Yes ;)

Edit: It's better to use the following code to get the cell, so you need just the one sub:

B4X:
Dim TC As TableCell = TableView1.GetItems(0).Get(0)

'Now call the sub from #5

Thanks Jan,

I tried it as you said :
B4X:
            TableView1.SetSelection(0,0)
            Dim tc As TableCell = TableView1.GetItems(0).Get(0)
            SetSelectedColor(tc,Colors.RGB(118,216,158))

this gives me nsobject not initialized at line
B4X:
    NaObj2.GetField("nativeCell").RunMethod("setSelectedBackgroundView:",Array(BgView))

if needed i can send the project by mail ?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Call TableView1.ReloadSection(0) before calling SetSelectedColor ;)
I have added the line. Now i am not getting error but the first cell is not selected. ( if i remove setselected and reload section it selects the first cell but with gray color :/ )

B4X:
            TableView1.SetSelection(0,0)
            Dim tc As TableCell = TableView1.GetItems(0).Get(0)
            TableView1.ReloadSection(0)
            SetSelectedColor(tc,Colors.RGB(118,216,158))
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,
sorry I was imprecise, call the methods in that order:
B4X:
TableView1.ReloadSection(0)
Dim tc As TableCell = TableView1.GetItems(0).Get(0)
SetSelectedColor(tc,Colors.RGB(118,216,158))
TableView1.SetSelection(0,0)

Jan
 
Upvote 0
Top