I’m building a little app where I show in a TableView an array. The problem is that I don’t want to use a UITableViewController, I just wanna use a ViewController. When I run the app in the simulator, it doesn’t show the array. Here’s the code:
.h
@interface ViewController : UIViewController{
IBOutlet UITableView *table;
NSArray *array;
}
.m
- (void)viewDidLoad
{
[super viewDidLoad];
array =[NSArray arrayWithObjects:@"iPad",@"iTV" ,nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
Please don’t answer this saying that I have to use a UITableViewController. I know is possible to manage this without it, just using a ViewController. Thank you anyway!
Define your interface like
and set
table‘sdelegateanddataSourceoutlet toFile's owner, andFile's owner‘s class asViewControllerin xib.First browse in your project, click the xib file for your view controller. Then select the table view from the list of objects. In this case, there may be some extra consideration:
If you have already created the xib with UITableViewController, you need to modify it to fit for UIViewController. Remember, the UIViewController subclass instance is shown as File’s owner object. You need to make sure that there is a top-level UIView object, which is not added as subview of any other view, and that there is a UITableView in the UIView.
The UIView should have a referencing outlet: view – File’s owner. If not, drag from the white circle of
New Referencing OutlettoFile's Owner, release mouse and then click onview.For the table view, click and drag from
New Referencing Outlet, and drop to File’s Owner, and clicktable, and drag fromdataSource,delegate, and drop to File’s Owner, respectively.BTW, it is recommended to use some capital characters as prefix for any class name.