iOS: Push array to UITableView
Can you run me through the steps to push an array to a UITableview?
I'm new t开发者_StackOverflowo Cocoa/Objective-C, so could you explain how the .m, .h work with the delegate?
Do this:
@interface SomeInstance : UITableViewController {
NSArray *theArray;
}
@end
@implementation SomeInstance
- (void)viewDidLoad {
theArray = [[NSArray alloc] initWithObjects:@"Apple",@"Pineapple",@"Banana",nil];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [theArray count];
}
- (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.textLabel.text = [theArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selected a row" message:[theArray objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)dealloc {
[theArray release];
[super dealloc];
}
@end
If you're new to ObjC, I would suggest starting with something more simple before diving into UITableViews.
(sorry if that's not what you want to hear, but I figure you'll be more productive in the long run).
To answer your specific question, A class that inherits from UITableViewControllerDelegate is expected to support various methods required by the UITableViewController protocol.
精彩评论