Skip to content

Instantly share code, notes, and snippets.

@motoishmz
Last active October 13, 2015 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motoishmz/4117862 to your computer and use it in GitHub Desktop.
Save motoishmz/4117862 to your computer and use it in GitHub Desktop.
//
// ListViewController.m
// TableVIewPrac5
//
// Created by 山田 慶 on 2012/11/12.
// Copyright (c) 2012年 山田 慶. All rights reserved.
//
#import "ListViewController.h"
@interface ListViewController () {
NSString *str;
NSArray *images;
}
@end
@implementation ListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//ラベル2用
str = @"あいうえおかきくけこ";
//画像データを配列へ読み込み
NSMutableArray* tempAr = [[NSMutableArray alloc]init];
for (int i = 0; i < 10; i++)
{
[tempAr addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]]];
}
images = [[NSArray alloc]initWithArray:tempAr];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
//テーブルセクション設定 :セクション数=1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//セクション内のMAXセル数を設定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// セルのリサイクル
static NSString *CellIdentifier = @"Cell";
customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (customCell == nil){
customCell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//■■■■■ イメージファイルをセット ■■■■■
UIImage* img = [images objectAtIndex:indexPath.row]; //viewDidLoadで読み込んだ画像データから該当の画像を取り出す
customCell.cityImgView.frame = CGRectMake(0, 0, img.size.width / 3, img.size.height / 3); //カスタムセル内のイメージビューの位置とサイズを画像にあわせて設定
customCell.cityImgView.center = CGPointMake(customCell.frame.size.width / 2, customCell.cityImgView.center.y); //中心位置を設定する。
customCell.cityImgView.image = img; //画像をイメージビューにセット
//ラベル1に値セット (インデックス)
customCell.customLbl1.text = [NSString stringWithFormat:@"%d",indexPath.row + 1];
//ラベル2に値セット (変数strより)
customCell.customLbl2.text = [str substringWithRange:NSMakeRange(indexPath.row, 1)];
return customCell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"テーブルビューの練習";
}
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UIImageの高さに基づいて、UITableViewCellの高さを設定。
UIImage* img = [images objectAtIndex:indexPath.row];
return img.size.height / 3;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment