Created
October 22, 2012 22:55
-
-
Save h5y1m141/3935254 to your computer and use it in GitHub Desktop.
QiitaのiPhoneアプリをRubyMotionで書いてみる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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