Forum Archive

cell.image_view Button.action

DavinE

Hello Guys,

Before I would now try again forever around a short question:
Is it possible to create a button in the cell.image_view with an action function BUT without triggering the action function of the TableView ?

cvp

@DavinE sure, but in the cell.content_view

b = ui.Button()
.
.
.
cell.content_view.add_subview(b)
DavinE

@cvp said:

@DavinE sure, but in the cell.content_view
b = ui.Button() . . . cell.content_view.add_subview(b)

Somehow it does not work for me....
Is that a problem when I use the "Title" and "Detail Title" that it is not displayed ?
Where a label is displayed...

I do not understand 😢

cvp

@DavinE no more time today but perhaps could you post your tableview_cell_for_row delegate

cvp

@DavinE try something like

import ui
from objc_util import *

class source (object):
    def tableview_number_of_rows(self, tv, s):
        return 4

    def tableview_cell_for_row(self, tv, s, r):
        cell = ui.TableViewCell(style='subtitle')
        w = cell.content_view.width
        h = cell.content_view.height
        b = ui.Button()
        b.frame = (10,0,h,h)
        b.image = ui.Image.named('emj:Baby_Chick_3').with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
        def b_action(sender):
          print('b_action')
        b.action = b_action
        cell.content_view.add_subview(b)
        x = b.x+b.width+10
        t = ui.Label()
        t.frame = (x,0,w-x,h/2)
        t.font = cell.text_label.font
        t.text = 'Title'
        cell.content_view.add_subview(t)
        st = ui.Label()
        st.font = cell.detail_text_label.font
        st.frame = (x,t.y+t.height+h/6,w-x,h/3)
        st.text = 'SubTitle'
        cell.content_view.add_subview(st)
        return cell

view = ui.TableView()

view.data_source = source()

view.present()
DavinE

@cvp said:

@DavinE no more time today but perhaps could you post your tableview_cell_for_row delegate

        cell = ui.TableViewCell('subtitle')

        if self.requestCustomers__selectCustomerMaterialsInsert[row][2]:
            data_textLabel = f'{self.requestCustomers__selectCustomerMaterialsInsert[row][4]}'
        else:
            data_textLabel = f'{self.requestCustomers__selectCustomerMaterialsInsert[row][4]}, {self.requestCustomers__selectCustomerMaterialsInsert[row][3]}'

        b = ui.Button()
        b.image = ui.Image('iow:ios7_star_outline_32')
        cell.content_view.add_subview(b)

        # 'iow:ios7_star_32'

        cell.text_label.text = data_textLabel
        cell.detail_text_label.text = self.requestCustomers__selectCustomerMaterialsInsert[row][5]

        # Style der cell
        style_cell(cell, self.DEVICE, 'tableview_cell_for_row')

        return cell

I had tried something in the direction but now that I see yours I would never have gotten there.....

many many thanks

DavinE

@cvp said:

@DavinE try something like
```
import ui
from objc_util import *

class source (object):
def tableview_number_of_rows(self, tv, s):
return 4

def tableview_cell_for_row(self, tv, s, r):
    cell = ui.TableViewCell(style='subtitle')
    w = cell.content_view.width
    h = cell.content_view.height
    b = ui.Button()
    b.frame = (10,0,h,h)
    b.image = ui.Image.named('emj:Baby_Chick_3').with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
    def b_action(sender):
      print('b_action')
    b.action = b_action
    cell.content_view.add_subview(b)
    x = b.x+b.width+10
    t = ui.Label()
    t.frame = (x,0,w-x,h/2)
    t.font = cell.text_label.font
    t.text = 'Title'
    cell.content_view.add_subview(t)
    st = ui.Label()
    st.font = cell.detail_text_label.font
    st.frame = (x,t.y+t.height+h/6,w-x,h/3)
    st.text = 'SubTitle'
    cell.content_view.add_subview(st)
    return cell

view = ui.TableView()

view.data_source = source()

view.present()
```

Thanks @cvp I'll try your code :D

DavinE

@cvp said:

@DavinE try something like
```
import ui
from objc_util import *

class source (object):
def tableview_number_of_rows(self, tv, s):
return 4

def tableview_cell_for_row(self, tv, s, r):
    cell = ui.TableViewCell(style='subtitle')
    w = cell.content_view.width
    h = cell.content_view.height
    b = ui.Button()
    b.frame = (10,0,h,h)
    b.image = ui.Image.named('emj:Baby_Chick_3').with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
    def b_action(sender):
      print('b_action')
    b.action = b_action
    cell.content_view.add_subview(b)
    x = b.x+b.width+10
    t = ui.Label()
    t.frame = (x,0,w-x,h/2)
    t.font = cell.text_label.font
    t.text = 'Title'
    cell.content_view.add_subview(t)
    st = ui.Label()
    st.font = cell.detail_text_label.font
    st.frame = (x,t.y+t.height+h/6,w-x,h/3)
    st.text = 'SubTitle'
    cell.content_view.add_subview(st)
    return cell

view = ui.TableView()

view.data_source = source()

view.present()
```

This works great @cvp Thank you very much

cvp

@DavinE you're always welcome

DavinE

@cvp Thanks very kind of you

DavinE

@cvp said:

@DavinE try something like
```
import ui
from objc_util import *

class source (object):
def tableview_number_of_rows(self, tv, s):
return 4

def tableview_cell_for_row(self, tv, s, r):
    cell = ui.TableViewCell(style='subtitle')
    w = cell.content_view.width
    h = cell.content_view.height
    b = ui.Button()
    b.frame = (10,0,h,h)
    b.image = ui.Image.named('emj:Baby_Chick_3').with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
    def b_action(sender):
      print('b_action')
    b.action = b_action
    cell.content_view.add_subview(b)
    x = b.x+b.width+10
    t = ui.Label()
    t.frame = (x,0,w-x,h/2)
    t.font = cell.text_label.font
    t.text = 'Title'
    cell.content_view.add_subview(t)
    st = ui.Label()
    st.font = cell.detail_text_label.font
    st.frame = (x,t.y+t.height+h/6,w-x,h/3)
    st.text = 'SubTitle'
    cell.content_view.add_subview(st)
    return cell

view = ui.TableView()

view.data_source = source()

view.present()
```

@cvp question, why doesn't it work with this code part anymore ?

st.number_of_lines = 0 
cvp

@DavinE said

question, why doesn't it work with this code part anymore ?

Sorry, I don't understand. What does not work with this line?

DavinE

@cvp said:

@DavinE said

question, why doesn't it work with this code part anymore ?

Sorry, I don't understand. What does not work with this line?

I Found my Solution:

       cell = ui.TableViewCell(style='subtitle')

        w = cell.content_view.width
        h = cell.content_view.height

        b = ui.Button()
        b.frame = (10, 0, h, h)
        b.image = ui.Image.named('iob:ios7_star_outline_32')
        b.tint_color = b.text_color = '#F1D565'

        def b_action(sender):
          print('b_action')
          print(r)
        b.action = b_action
        cell.content_view.add_subview(b)

        x = b.x+b.width+10

        t = ui.Label()
        t.frame = (x,8,(tv.width - (b.width * 2)),h/2)
        t.font = ('Anonymous Pro', 16)
        t.text = 'Ich bin der sehr sehr sehr lange Text um etwas zu testen ob es nun so geht oder nicht reicht der Text denn?'
        t.number_of_lines = 0
        t.size_to_fit()
        cell.content_view.add_subview(t)

        st = ui.Label()
        st.font = ('Anonymous Pro', 13)
        st.x = x
        st.y = t.y+t.height+h/15
        st.width = w-x
        st.frame = (x,t.y+t.height+h/25,(tv.width - (b.width * 2)),h/3)
        st.text = 'Ich bin der sehr sehr sehr lange Text um etwas zu testen ob es nun so geht oder nicht reicht der Text denn?'
        st.number_of_lines = 0
        st.size_to_fit()
        cell.content_view.add_subview(st)

        tv.row_height = (t.height + st.height + 19)
        b.y = ((tv.row_height - b.height) / 2)
        return cell 

But I don't know if the code is written that well 😂.

My problem was that it does not support multiple lines

cvp

@DavinE Said

My problem was that it does not support multiple lines

The frame of both titl and subtitle are not high enough for that.

Problem is that y of subtitle is function of height of title and with size_to_fit, this height can increase outside of the cell

Try with

        t = ui.Label()
        t.frame = (x,0,(tv.width - (b.width * 2)),2*h/3)
        t.font = ('Anonymous Pro',14)
        t.text = 'Ich bin der sehr sehr sehr lange Text um etwas zu testen ob es nun so geht oder nicht reicht der Text denn?'
        t.number_of_lines = 0
        #t.size_to_fit()
        cell.content_view.add_subview(t)
        st = ui.Label()
        st.font = ('Anonymous Pro', 10)
        st.frame = (x,2*h/3,(tv.width - (b.width * 2)),h/3)
        st.text = 'Ich bin der sehr sehr sehr lange Text um etwas zu testen ob es nun so geht oder nicht reicht der Text denn?'
        st.number_of_lines = 0
        #st.size_to_fit()
        cell.content_view.add_subview(st)

DavinE

@cvp Ah okay, now I know why it didn't work.
Thank you 👍