I love using Interface Builder– I know, I lose geek points by saying that. So I finally figured out this thing I’d always wanted to do: instead of just using an entire view as a UITableViewController, what if you want to use it like any other UI object, as a object within a View, and my class can remain UIViewController? I got it to work, this is how:
To a regular view.
1. Drag the UITableView object over to the view.
2. View “connections” pain, and drag delegate and datasource over to “File Owner”.
3. In your code editor, go into the View Controller, and add these protocols: “UITableViewDelegate,UITableViewDataSource”
@interface friendsVC : UIViewController < UITableViewDelegate,UITableViewDataSource>
{
}
4. Add a property for the UITableView. I call it “myTV”. Remember to synthesize and release.
@interface friendsVC : UIViewController < UITableViewDelegate,UITableViewDataSource>
{
IBOutlet UITableView *myTV;
}
@property (nonatomic, retain) IBOutlet UITableView *myTV;
5. In Interface Builder, select the File Owner (gold box icon on left), and select the “Connections” pane on your right. Make a connection between myTV and the actual UITableView in the view.
6. Now, include various methods inside your *.m file, so you can leverage the loveliness that is UITableViews
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [put number here];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [number of rows];
}
- (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];
}
// Configure the cell.
cell.textLabel.text = @"";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
/* statements here */
}
Hope this was helpful!