- import objc
- import _uicaboodle
- from objc import YES, NO
- from sqlite3 import dbapi2 as sqlite
- objc.loadBundle('UIKit', globals(), '/System/Library/Frameworks/UIKit.framework')
- class PYApplication(UIApplication):
- def applicationDidFinishLaunching_(self, unused):
- self.contacts = []
- self.sections = []
- frame = UIHardware.fullScreenApplicationContentRect()
- self.window = UIWindow.alloc().initWithFrame_(frame)
- self.view = UIView.alloc().initWithFrame_(self.window.bounds())
- self.window._.contentView = self.view
- self.window.orderFront_(self)
- self.window.makeKey_(self)
- self.window._setHidden_(NO)
- navsize = UINavigationBar.defaultSize()
- navrect = ((0, 0), navsize)
- self.navbar = UINavigationBar.alloc().initWithFrame_(navrect)
- self.view.addSubview_(self.navbar)
- self.navbar._.barStyle = 1
- self.navbar._.delegate = self
- navitem = UINavigationItem.alloc().initWithTitle_(u"Contacts")
- self.navbar.pushNavigationItem_(navitem)
- bounds = self.view.bounds()
- tblrect = ((0, navsize[1]), (bounds[1][0], bounds[1][1] - navsize[1]))
- self.loadContactsFromDB()
- self.secList = UISectionList.alloc().initWithFrame_(tblrect)
- self.secList._.dataSource = self
- self.secList.reloadData()
- col = UITableColumn.alloc().initWithTitle_identifier_width_(u"Name", u"name", 320.0)
- table = self.secList.table()
- table._.separatorStyle = 1
- table._.delegate = self
- table.addTableColumn_(col)
- self.view.addSubview_(self.secList)
- def loadContactsFromDB(self):
- db = sqlite.connect("/private/var/mobile/Library/AddressBook/AddressBook.sqlitedb") #FIXME: get proper path
- cursor = db.cursor()
- cursor.execute("select first, last from ABPerson where first is not null order by first")
- row = 0
- lastInitial = u""
- for first, last in cursor.fetchall():
- self.contacts.append({"first": (first or u""), "last": (last or u""), "cell": None})
- thisInitial = unicode(first[0])
- if thisInitial != lastInitial:
- self.sections.append((thisInitial, row))
- lastInitial = thisInitial
- row += 1
- cursor.close()
- db.close()
- @objc.signature("I@:@")
- def numberOfSectionsInSectionList_(self, sectionList):
- print u"numberOfSectionsInSectionList_", sectionList
- return len(self.sections)
- @objc.signature("@@:@I")
- def sectionList_titleForSection_(self, sectionList, section):
- print u"sectionList_titleForSection_", sectionList, section
- return self.sections[section][0]
- @objc.signature("I@:@I")
- def sectionList_rowForSection_(self, sectionList, section):
- print u"sectionList_rowForSection_", sectionList, section
- return self.sections[section][1]
- @objc.signature("I@:@")
- def numberOfRowsInTable_(self, table):
- print u"numberOfRowsInTable_", table
- return len(self.contacts)
- @objc.signature("@@:@I@")
- def table_cellForRow_column_(self, table, row, column):
- print u"table_cellForRow_column_", table, row, column
- if not self.contacts[row]['cell']:
- cell = UIImageAndTextTableCell.alloc().init()
- cell._.title = "%s %s" % (self.contacts[row]['first'], self.contacts[row]['last'])
- self.contacts[row]['cell'] = cell
- return self.contacts[row]['cell']
- @objc.signature("B@:@I")
- def table_canSelectRow_(self, table, row):
- return NO
- @objc.signature("B@::")
- def respondsToSelector_(self, selector):
- responds = dir(self).count(selector.replace(":", "_")) > 0
- if not responds:
- print selector
- return responds
- _uicaboodle.UIApplicationMain(['HelloPython'], PYApplication)
You must be logged in to paste new items to the PasteBin
