@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
// ADDED
NSArray *array;
IBOutlet UITableView *tableView;
}
@property (nonatomic, retain) NSArray *array;
@end
// in table view controller .m
@implementation ViewController
@synthesize array;
// ADDED: method to handle table cell click
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"click cell: %i", indexPath.row);
// show another VC
AboutVC *avc = [[AboutVC alloc] initWithNibName:@"AboutVC" bundle:nil];
[self.view addSubview:avc.view];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
// ADDED
cell.textLabel.text = [self.array objectAtIndex:indexPath.row]; // text label of the cell
cell.imageView.image = [UIImage imageNamed:@"myimage.gif"]; // image on the left
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; // show a '>' button
// end
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableView.delegate = self;
tableView.dataSource = self;
// ADDED
self.array = [NSArray arrayWithObjects:@"Beng", @"Seng", @"Lian", nil];
}
No comments:
Post a Comment