iOS Dev (13) UITableView
iOS Dev (13) UITableView
- 作者:CSDN 大锐哥
- 地址:http://blog.csdn.net/prevention
AppDelegate
.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *vc;
@end
.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = self.vc;
[self.window makeKeyAndVisible];
[self.addSubView self.vc];
}
ViewController
.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
.m
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
NSArray *dataArray;
}
@end
@implementation ViewController
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
// self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
// if (self) {
// // Custom initialization
// }
// return self;
//}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
CGRect frame = [self.view bounds];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
dataArray = [NSArray arrayWithObjects:@"Google", @"Facebook", @"Twitter", @"Amazon", @"Ebay", @"Apple", @"Tencent", @"Adobe", nil];
[tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -----
#pragma mark UITableViewSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"UITableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
#pragma mark -----
#pragma mark UITableViewDelegate
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", [dataArray objectAtIndex:indexPath.row]);
}
@end