Skip to content

Instantly share code, notes, and snippets.

@keicoder
Created January 29, 2014 06:23
Show Gist options
  • Save keicoder/8682867 to your computer and use it in GitHub Desktop.
Save keicoder/8682867 to your computer and use it in GitHub Desktop.
objective-c : iOS Simple TableView
//iOS Simple TableView
//You have to wire up data Source and delegate to the tableView in the xib file
//ViewController.h
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *tableData; // holds the table data (title)
@property (nonatomic, strong) NSMutableArray *tableDetailData; // holds the table data (detail text)
@end
//ViewController.m
#import "ViewController.h"
static NSString *cellIdentifier = @"Cell";
@interface ViewController ()
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - 뷰 라이프 사이클
- (void)viewDidLoad
{
[super viewDidLoad];
// Create the array to hold the table data
self.tableData = [[NSMutableArray alloc] init];
self.tableDetailData = [[NSMutableArray alloc] init];
for (NSUInteger i=0; i < 10; i++) {
NSString *tableData = [NSString stringWithFormat:@"Item %d", i];
NSString *tableDetailData = [NSString stringWithFormat:@"Detail Item %d", i];
[self.tableData addObject:tableData];
[self.tableDetailData addObject:tableDetailData];
}
NSLog(@"The tableData array contains %@", self.tableData);
NSLog(@"The tableDetailData array contains %@", self.tableData);
}
#pragma mark - 테이블 뷰 데이터 소스
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row]; // 셀 타이틀 텍스트
cell.detailTextLabel.text = [self.tableDetailData objectAtIndex:indexPath.row];; // 셀 디테일 텍스트
return cell;
}
#pragma mark - 테이블 뷰 델리게이트
#pragma mark 행 선택
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Table row %d has been tapped", indexPath.row);
//경고 창
NSString *messageString = [NSString stringWithFormat:@"You tapped row %d",indexPath.row];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row tapped"
message:messageString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
#pragma mark 행 높이
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment