Skip to content

Instantly share code, notes, and snippets.

@h5y1m141
Created October 22, 2012 22:55
Show Gist options
  • Save h5y1m141/3935254 to your computer and use it in GitHub Desktop.
Save h5y1m141/3935254 to your computer and use it in GitHub Desktop.
QiitaのiPhoneアプリをRubyMotionで書いてみる
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds).tap do |window|
window.rootViewController = UINavigationController.alloc.initWithRootViewController(TableViewController.new)
window.makeKeyAndVisible
end
true
end
end
# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require 'bubble-wrap'
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'Qiita'
end
class TableViewController < UITableViewController
def viewDidLoad
super
@feed = nil
self.navigationItem.title = "Qiita Reader"
self.view.backgroundColor = UIColor.whiteColor
url = "https://qiita.com/api/v1/items"
BW::HTTP.get(url) do |response|
if response.ok?
@feed = BW::JSON.parse(response.body.to_str)
view.reloadData
else
App.alert(response.error_message)
end
end
end
def tableView(tableView, numberOfRowsInSection:section)
if @feed.nil?
return 0
else
@feed.size
end
end
def tableView(tableView, heightForRowAtIndexPath:indexPath)
60
end
def tableView(tableView, cellForRowAtIndexPath:indexPath)
cell = tableView.dequeueReusableCellWithIdentifier('cell') || UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:'cell')
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator
label = UILabel.alloc.init
label.frame = CGRectMake(5, 40, 200, 30)
label.font = UIFont.fontWithName("AppleGothic",size:14)
label.text = @feed[indexPath.row]["title"]
# label.text = @feed[indexPath.row]["user"]["profile_image_url"]
label.textAlignment = UITextAlignmentCenter
cell.addSubview(label)
image_path = @feed[indexPath.row]["user"]["profile_image_url"]
image_src = NSData.dataWithContentsOfURL(NSURL.URLWithString(image_path))
image = UIImage.imageWithData(image_src)
image_view = UIImageView.alloc.initWithImage(image)
image_view.frame = CGRectMake(5, 5, 30, 30)
cell.addSubview(image_view)
return cell
end
def tableView(tableView, didSelectRowAtIndexPath:indexPath)
WebViewController.new.tap do |c|
c.item = @feed[indexPath.row]
self.navigationController.pushViewController(c, animated:true)
end
end
end
class WebViewController < UIViewController
attr_accessor :item
def viewDidLoad
super
self.navigationItem.title = self.item["title"]
@webview = UIWebView.new.tap do |v|
v.frame = self.view.bounds
v.scalesPageToFit = true
v.loadHTMLString(self.item["body"], baseURL:nil)
v.delegate = self
view.addSubview(v)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment