Skip to content

Instantly share code, notes, and snippets.

@cvpe
Created October 6, 2020 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvpe/f522f1b25961cf94e0e045a39ff90fb7 to your computer and use it in GitHub Desktop.
Save cvpe/f522f1b25961cf94e0e045a39ff90fb7 to your computer and use it in GitHub Desktop.
MyUITableView.py
# 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_viewForHeaderInSection_(self,cmd,tableView,section):
print('ici')
return ns('Header for ' + list(data.keys())[section]).pt
def tableView_heightForHeaderInSection_(self,cmd,tableView,section):
print('ici')
return 50.0
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_]#, tableView_viewForHeaderInSection_, tableView_heightForHeaderInSection_]
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 None
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment