0
votes

I am creating a view at runtime and adding a UITableView as its subview. I have also defined the delegate of the tableview to self .

But still the UITableView Delegates are not getting called

@interface cViewController : UIViewController

RouteShowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[RouteShowView setBackgroundColor:[UIColor blueColor]];

table = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, 320, 410) style:UITableViewStylePlain];
table.delegate = self;
UIButton * Go = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Go.frame = CGRectMake(0, 4, 70, 42);
[Go setTitle:@"<< BACK" forState:UIControlStateNormal];
[Go addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[Go setBackgroundColor:[UIColor blackColor]];

[RouteShowView addSubview:table];
[RouteShowView addSubview:Go];

[self.view addSubview:RouteShowView]; 
3
I'm guessing because a UITableView's datasource is more important and it is required. Try with table.datasource=self. and don't forget to add methods cellForRowAtIndexPath and numberOfRowsInSection - iNoob
no luck for this also :( - WakkaoW
@WakkaoW No luck with trying datasource also ? - iNoob
@iNoob Yes, Still no luck .....but thanks datasource is needed any way. But still numberOfRowsInSection is not getting called - WakkaoW
@WakkaoW, that is weird. I just tried with including datasource and returning number of rows as 3. Though i tried it for self.view addSubView:table instead of your RouteShowView . - iNoob

3 Answers

2
votes

Assuming that you added UITableViewDelegate, UITableViewDataSource in your .h file... i think you missed this code in your .m file

table.dataSource = self;
1
votes

Missing things are

//.h file
@interface SimpleTableView : UITableView <UITableViewDelegate, UITableViewDataSource>
{
}
@end

// in .m file
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, 320, 410) style:UITableViewStylePlain];

// set delegated object
table. delegate = self;

// set source for data
table.dataSource = self;

And implement @required protocols which are defined under documentation of UITableView

@protocol UITableViewDataSource<NSObject>

@required

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
0
votes

First try checking the delegates methods by code completion on xcode, If you obtained proper delegates into your class, xcode automatically detects your delegate methods.

I guess you may not be declare

@interface SimpleTableView : UITableView <UITableViewDelegate, UITableViewDataSource>