まず、xibファイルにTableViewを置きます。
TableViewは、40・90に置いて、サイズは240・280に。
TableViewは、outletsで、delegateとdataSourceをそれぞれFile's Ownerに接続して下さい。
のちほど、@propertyを宣言した後、myTableViewとOutletsを接続してください。
ViewController.h
#import <UIKit/UIKit.h>
#define TABLE_NUMBER 20
@interface ViewController : UIViewController
<
UITableViewDelegate,
UITableViewDataSource
>
{
IBOutlet UIScrollView *scrollView;
UIImageView *imageView;
UILabel *rankLabel;
UITextField *passTextFld;
UITableView *myTableView;
}
@property (nonatomic, retain) IBOutlet UITableView *myTableView;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController (){
NSMutableArray *_objects;
}
@end
@implementation ViewController
@synthesize myTableView;
- (void)dealloc
{
[_objects release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myTableView.allowsSelection = NO; //セルの選択の可否設定 NO:選択不可
}
#pragma mark Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return TABLE_NUMBER;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// セルが選択された時のハイライトなし
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
for(int i=0; i
int j = ((i+1)/10);
if(j < 1){
[_objects insertObject:[NSString stringWithFormat:@"RANK : %d", i+1] atIndex:i];
}else {
[_objects insertObject:[NSString stringWithFormat:@"RANK : %d", i+1] atIndex:i];
}
}
NSString *object = [_objects objectAtIndex:indexPath.row]; //row番目のデータに_objcetをセット
cell.textLabel.text = [object description];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
#pragma mark セルの色
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
cell.backgroundColor = [UIColor whiteColor];
}
// For odd
else {
cell.backgroundColor = [UIColor colorWithHue:0.61
saturation:0.09
brightness:0.99
alpha:1.0];
}
}
#pragma mark テーブルビューの高さ
//#define row_height 25
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 25.0f;
}
#pragma mark テーブルが選択された時の処理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES]; //必須メソッド
//セルが選択されたときの処理
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
分かりづらいと思いますが、これで出来ます。
20個のテーブルに、 RANK :1 ~ RANK :20と表示されます。
何を表示するかは自分で設定してください。
そこそこ時間がかかったのですが、自分でできたので、
嬉しくて載せてみました。
例えば、RANKが10で、画面遷移した時に、そこを中心に表示できるのかとか、
そこだけ色を変えれるのかとかは調べないので、また明日調べます。
それで使えなかったら笑えますけど、それはそれでその時考えます。
ご参考までに。