Creating a simple Table View
- Open the storyboard, from the right panel, drag a Table View control onto the story board.
- Open ViewController.h and add the highlighted text:
@interface XViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end - Open ViewController.m and insert the following line in between the @implementation section:
NSArray *tableData; - Add the following code in the viewDidLoad function:
tableData = [NSArray arrayWithObjects:@"Item 1", @"Item 2", @"Item 3", @"Item4", nil]; - Add the following 2 functions as well:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return [tableData count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *simpleTableIdentifier = @"SimpleTableItem";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];}cell.textLabel.text = [tableData objectAtIndex:indexPath.row];return cell;}
- Open storyboard
- Right click drag from the TableView control to the yellow icon and release. Choose dataSource.
- Done. Run the app:
No comments:
Post a Comment