Skip to content

Commit

Permalink
tableViewCellの再利用処理修正
Browse files Browse the repository at this point in the history
  • Loading branch information
toshikazuhorii committed Dec 10, 2013
1 parent 8aaf92b commit 7811b96
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.repl_history
build
tags
app/pixate_code.rb
resources/*.nib
resources/*.momd
resources/*.storyboardc
Expand All @@ -11,5 +12,6 @@ nbproject
*~
*.sw[po]
.eprj
.idea/*
.dat*
.sass-cache
.idea
Gemfile.lock
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'rake'

gem 'bubble-wrap'
10 changes: 7 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require 'bubble-wrap'
require 'motion/project/template/ios'

begin
require 'bundler'
Bundler.require
rescue LoadError
end

Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'MotionExample'
end
32 changes: 20 additions & 12 deletions app/controllers/ExampleController.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ExampleController < UIViewController
CELL_REUSE_IDENTIFIER = 'cell'
CELL_REUSE_IDENTIFIER = 'Items'

def viewDidLoad
super
Expand All @@ -20,26 +20,34 @@ def viewDidLoad
end

def tableView(tableView, cellForRowAtIndexPath:indexPath)
cell = tableView.dequeueReusableCellWithIdentifier(CELL_REUSE_IDENTIFIER) || begin
UITableViewCell.alloc.initWithStyle(
cell = tableView.dequeueReusableCellWithIdentifier(CELL_REUSE_IDENTIFIER)

if cell.nil?
cell = ExampleCell.alloc.initWithStyle(
UITableViewCellStyleDefault,
reuseIdentifier:CELL_REUSE_IDENTIFIER
)

# thumbnail
image_view = UIImageView.alloc.initWithFrame(CGRectMake(5, 5, 32, 32))
cell.thumbnail = image_view
cell.addSubview(image_view)

# text label
label = UILabel.alloc.initWithFrame(CGRectMake(45, 10, 250, 20))
label.font = UIFont.boldSystemFontOfSize(12)
cell.label = label
cell.addSubview(label)
end

item = @data[indexPath.row]

# text label
# thumbnail
image_data = NSData.dataWithContentsOfURL(NSURL.URLWithString(item[:image_url]))
image_view = UIImageView.alloc.initWithImage(UIImage.imageWithData(image_data))
image_view.frame = CGRectMake(5, 5, 32, 32)
cell.addSubview(image_view)
cell.thumbnail.image = UIImage.imageWithData(image_data)

# thumbnail
label = UILabel.alloc.initWithFrame(CGRectMake(45, 10, 250, 20))
label.font = UIFont.boldSystemFontOfSize(12)
label.text = item[:name]
cell.addSubview(label)
# text label
cell.label.text = item[:name]

return cell
end
Expand Down
4 changes: 4 additions & 0 deletions app/views/ExampleCell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ExampleCell < UITableViewCell
attr_accessor :thumbnail
attr_accessor :label
end

0 comments on commit 7811b96

Please sign in to comment.