Reputation: 3655
I'm want to put a tableView inside a UIViewController, because I need a toolbar in the top of the view and i therefor can't use the tableViewController. I have created a tableView class and put the what i thought would be the needed functions inside it, but i must be missing something, or have put something wrong somewhere.
.h
import <UIKit/UIKit.h>
@interface TestTV : UITableView
@property(strong,nonatomic)NSArray *arr;
@end
.m
#import "TestTV.h"
@implementation TestTV
@synthesize arr = _arr;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
_arr = [[NSArray alloc]initWithObjects:@"HEJ",@"FOO", nil];
return _arr.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];
}
NSString *cellValue = [_arr objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
@end
Upvotes: 2
Views: 5604
Reputation: 31782
You probably don't want to subclass UITableView
. Instead, in your view controller subclass, declare your intention to implement the appropriate delegate and data source protocol:
@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
Then, in the implementation file of your view controller, implement the methods you've defined above.
Finally, in Interface Builder (or programmatically) set both the delegate
and dataSource
outlets of the table view to be equal to its superview's view controller (in IB, this view controller is File's Owner).
You can make the data array a property of the view controller as well.
Upvotes: 6