Forum Archive

Ui tableViewCell

Drizzel

I have some questions about tableViewCells:

  • when I swipe a tableViewCell to the left, it displays a red delete button. Is it possible to show multiple buttons there?
  • can I see for how long a tableViewCell has been pressed? Like, if I hold down on it for two seconds I get a menu to rename that cell?
  • Is it possible to display a second line of text in a tableViewCell with a different Color and size?

Thanks a lot :)

cvp

@Drizzel said:

Is it possible to display a second line of text in a tableViewCell with a different Color and size?

Quick and dirty code only to show ingredients

import ui

def tableview_cell_for_row(tableview, section, row):
    cell = ui.TableViewCell()
    data = tableview.data_source.items[row]
    cell.text_label.text = ''
    h = tableview.row_height
    l1 = ui.Label()
    l1.frame = (4,0,cell.width,h/2)
    l1.text = 'line 1 of ' + data
    l1.text_color = 'blue'
    l1.font = ('Menlo',16)
    cell.content_view.add_subview(l1)
    l2 = ui.Label()
    l2.frame = (4,h/2,cell.width,h/2)
    l2.text = 'line 2 of ' + data
    l2.text_color = 'red'
    l2.font = ('Menlo',10)
    cell.content_view.add_subview(l2)
    return cell

lst = ['1','2','3']
elements_lst = ui.ListDataSource(items=lst)

tv = ui.TableView()
tv.data_source = elements_lst
tv.data_source.tableview_cell_for_row = tableview_cell_for_row
tv.row_height = 60

tv.frame = (0,0,300,400)
tv.name ='TableView with multiple lines'
tv.present('sheet') 
cvp

@Drizzel said:

can I see for how long a tableViewCell has been pressed? Like, if I hold down on it for two seconds I get a menu to rename that cell?

Thanks to Gestures module of @mikael, you could check long press duration, like here

from   Gestures import Gestures
.
.
.

tv.present('sheet')

def did_long_press(data):
    global t0
    if data.state == Gestures.BEGAN:
        t0 = datetime.datetime.now() 
    elif data.state == Gestures.ENDED:
        print(datetime.datetime.now()-t0)

g = Gestures()
g.add_long_press(tv,did_long_press, minimum_press_duration=0.5) 
cvp

@Drizzel said:

when I swipe a tableViewCell to the left, it displays a red delete button. Is it possible to show multiple buttons there?

That should be but needs ObjectiveC code

Drizzel

Fantastic, thanks @cvp and @mikael
I kind of miss the dog in your previous profile picture, though 😄

cvp

@Drizzel said:

I kind of miss the dog in your previous profile picture, though

I don't understand but I never see my profile picture updated...

cvp

@Drizzel and the winner is

with

# coding: utf-8
# https://gist.github.com/shaun-h/da54aafc2aeaf5a316e8de1b56d19fd3
from __future__ import absolute_import
from objc_util import *
import ui
from collections import OrderedDict
from six.moves import range
import console

UITableView = ObjCClass('UITableView')
UITableViewCell = ObjCClass('UITableViewCell')

class UITableViewStyle(object):
        UITableViewStylePlain = 0
        UITableViewStyleGrouped = 1

def test_data(number):
    data = OrderedDict()
    valueRange = 7
    for i in range(0,number):
        ii = i * valueRange
        key = str(ii)
        value = []
        for j in range(ii,ii+valueRange):
            value.append(str(j))
        data[key] = value
    return data

data = test_data(1)

def tableView_cellForRowAtIndexPath_(self,cmd,tableView,indexPath):
    ip = ObjCInstance(indexPath)
    cell = ObjCInstance(tableView).dequeueReusableCellWithIdentifier_('mycell')

    if cell == None:
        cell = UITableViewCell.alloc().initWithStyle_reuseIdentifier_(0,'mycell')
    key = list(data.keys())[ip.section()]
    text = ns(data[key][ip.row()])
    cell.textLabel().setText_(text)

    return cell.ptr

def numberOfSectionsInTableView_(self,cmd,tableView):
    return len(data)

def tableView_numberOfRowsInSection_(self,cmd, tableView,section):
    key = list(data.keys())[section]
    return ns(len(data[key])).integerValue()

def sectionIndexTitlesForTableView_(self,cmd,tableView):
    return ns(list(data.keys())).ptr

def tableView_sectionForSectionIndexTitle_atIndex_(self,cmd,tableView,title,index):
    #I have assumed order and number of list is the same from list and sections
    return index

def tableView_titleForHeaderInSection_(self,cmd,tableView,section):
    return ns('Header for ' + list(data.keys())[section]).ptr

def tableView_titleForFooterInSection_(self,cmd,tableView,section):
    return ns('Footer for ' + list(data.keys())[section]).ptr

def tableView_commitEditingStyle_forRowAtIndexPath_(self,cmd,tableView,editingStyle,indexPath):
    if editingStyle == 1:
        # delete
        section_row = ObjCInstance(indexPath)
        tv = ObjCInstance(tableView)
        row = section_row.row()
        b = console.alert('delete row',str(row), 'confirm', 'cancel', hide_cancel_button=True)
        if b == 1:
            pass
            #tv.beginUpdates()
            #?.removeAtIndex(indexPath.row)
            #tv.deleteRowsAtIndexPaths_withRowAnimation_([section_row],0)
            #tv.endUpdates()            


#def tableView_canEditRowAtIndexPath_(self,cmd,tableView,indexPath):
    #pass

#def tableView_canMoveRowAtIndexPath_(self,cmd,tableView,indexPath):
    #pass

#def tableView_moveRowAtIndexPath_toIndexPath_(self,cmd,tableView,fromIndexPath,toIndexPath):
    #pass

methods = [tableView_cellForRowAtIndexPath_,tableView_numberOfRowsInSection_,numberOfSectionsInTableView_,tableView_titleForHeaderInSection_,tableView_sectionForSectionIndexTitle_atIndex_,sectionIndexTitlesForTableView_,tableView_titleForFooterInSection_, tableView_commitEditingStyle_forRowAtIndexPath_]
protocols = ['UITableViewDataSource']
TVDataSourceAndDelegate = create_objc_class('TVDataSourceAndDelegate', NSObject, methods=methods, protocols=protocols)

#=============== TableView delegate: begin
def tableView_trailingSwipeActionsConfigurationForRowAtIndexPath_(self,cmd, tableView, indexPath):
    global UISwipeActionsConfiguration
    return UISwipeActionsConfiguration.ptr

methods = [tableView_trailingSwipeActionsConfigurationForRowAtIndexPath_]
protocols = ['UITableViewDelegate']
UITableViewDelegate = create_objc_class('UITableViewDelegate', NSObject, methods=methods, protocols=protocols)
#=============== TableView delegate: end

def handler():
    print('handler called')
    return


class MyTableView(ui.View):
    @on_main_thread
    def __init__(self, *args, **kwargs):
        ui.View.__init__(self, *args, **kwargs)
        global UISwipeActionsConfiguration
        frame = CGRect(CGPoint(0, 0), CGSize(self.width, self.height))
        self.tableView = UITableView.alloc().initWithFrame_style_(frame, UITableViewStyle.UITableViewStylePlain)
        flex_width, flex_height = (1<<1), (1<<4)
        self.tableView.setAutoresizingMask_(flex_width|flex_height)
        #print(dir(self.tableView))

        #=============== TableView delegate: begin              
        #set delegate
        self.tb_ds = TVDataSourceAndDelegate.alloc().init().autorelease()
        self.tableView.setDataSource_(self.tb_ds)

        self.tb_dl = UITableViewDelegate.alloc().init().autorelease()
        self.tableView.setDelegate_(self.tb_dl)

        # set actions if swipe
        UIContextualAction = ObjCClass('UIContextualAction').alloc()
        UIContextualAction.setTitle_("Drizzel's action 😅")
        # block does not have parameter nor return, thus we can use a Python def
        UIContextualAction.setHandler_(handler)
        UIContextualAction.setBackgroundColor_(ObjCClass('UIColor').blueColor().colorWithAlphaComponent(0.5))

        UISwipeActionsConfiguration = ObjCClass('UISwipeActionsConfiguration').configurationWithActions_([UIContextualAction])
        #=============== TableView delegate: end

        self_objc = ObjCInstance(self)
        self_objc.addSubview_(self.tableView)
        self.tableView.release()

if __name__ == '__main__':
    wv = MyTableView()
    wv.name = 'for @Drizzel eyes only 😀'
    wv.present() 
Drizzel

@cvp thanks a lot, that’s a long piece of code😅

I don't understand but I never see my profile picture updated...

So you didn’t realise your profile picture changed?

cvp

@Drizzel long piece of code but only small part added to the old one I wrote for your some time ago

About my profile picture, I had modified it but I never seen it changed when I connect to the forum. That's why I didn't understand that you have seen it modified.