(English) Grouped TableView Sample

(This entry is English translate of previous post.)
HelloPython, sample program of python on iPhone, is nice demonstration of how python code runs on iPhone. but I wanted more practical sample like this. (with some buttons, labels...)

so, I made one.

This uses TableView with GroupedStyle (UITableViewStyleGrouped).
To use, you can paste this code into HelloPython.py.

and in the middle of this code, there is "self.sections_" settings like this.

self.sections_ = [
{"header": "header 1", "items": [{"title": "title 1", "action": lambda p: self.callback(p)}]},
{"header": "header 2", "items": [
    {"title": "title 2-1", "action": None},
    {"title": "title 2-2", "action": None}]
}
]

You can configure the application by changing this settings. (clicking on "title 1" shows alert dialog by defalt.)

Enjoy iPhone programing.

(Ado Nishimura - email: ado at sig dot or dot jp)

  • Grouped TableView Sample code in Python.
import sys

import objc
from _uicaboodle import UIApplicationMain
from objc import YES, NO, NULL

objc.loadBundle("UIKit", globals(), "/System/Library/Frameworks/UIKit.framework")

class PYApplication(UIApplication):

    @objc.signature("i@:@")
    def numberOfSectionsInTableView_(self, table):
        return len(self.sections_)

    @objc.signature("i@:@i")
    def tableView_numberOfRowsInSection_(self, table, section):
        return len(self.sections_[section]["items"])

    @objc.signature("@@:@@")
    def tableView_cellForRowAtIndexPath_(self, table,  path):
        cell = table.dequeueReusableCellWithIdentifier_("PYApplication")
        if cell == None:
            zrect = ((0, 0), (0, 0))
            cell =  UITableViewCell.alloc().initWithFrame_reuseIdentifier_(zrect, "PYApplication")
        cell.setText_(self.sections_[path.section()]["items"][path.row()]["title"])
        return cell

    @objc.signature("@@:@i")
    def tableView_titleForHeaderInSection_(self, table, section):
        return self.sections_[section]["header"]

    @objc.signature("v@:@@")
    def tableView_didSelectRowAtIndexPath_(self, table, path):
        action = self.sections_[path.section()]["items"][path.row()]["action"]
        if action != None:
            action(path)

    def callback(self, path):
        UIAlertView.alloc().initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles_("", "You cliked on a TableView", None, "Ok", None).show()

    @objc.signature("v@:@")
    def applicationDidFinishLaunching_(self, unused):
        self.sections_ = [{"header": "header 1", "items": [{"title": "title 1", "action": lambda p: self.callback(p)}]},
                          {"header": "header 2", "items": [{"title": "title 2-1", "action": None},
                                                           {"title": "title 2-2", "action": None}]
                          }
                         ]

        # init window.
        outer = UIHardware.fullScreenApplicationContentRect()
        self.window = UIWindow.alloc().initWithFrame_(outer)

        self.window.orderFront_(self)
        self.window.makeKey_(self)
        self.window.setHidden_(NO)

        inner = self.window.bounds()
        navsize = UINavigationBar.defaultSize()
        navrect = ((0, 0), (inner[1][0], navsize[1]))

        # init UIView
        self.view = UIView.alloc().initWithFrame_(self.window.bounds())
        self.window.setContentView_(self.view)

        # init Navi-bar
        self.navbar = UINavigationBar.alloc().initWithFrame_(navrect)
        self.view.addSubview_(self.navbar)

        self.navbar.setBarStyle_(1)
        navitem = UINavigationItem.alloc().initWithTitle_("Grouped TableView")
        self.navbar.pushNavigationItem_(navitem)

        # init Table View
        lower = ((0, navsize[1]), (inner[1][0], inner[1][1] - navsize[1]))
        table = UITableView.alloc().initWithFrame_style_(lower, 1) # grouped TableView 
        self.table = table
        self.view.addSubview_(table)

        table.setSeparatorStyle_(1)
        table.setDelegate_(self)
        table.setDataSource_(self)
        table.reloadData()

UIApplicationMain(sys.argv, PYApplication)