denniss
denniss

Reputation: 17589

Creating a custom UITableView

I am trying to create a custom UITableView that is contained within another view. I have been able to get the view set up almost like the way that I want it. I am just wondering however how come the two methods (tableView:cellForRowAtIndexPath and tableView:didSelectRowAtIndexPath) that I need the most are not getting called. Note that I am not using UITableViewController, but I am using UIViewController to control the tableView.

enter image description here

And here is a snippet of my code

- (void)loadView {
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    UIView * contentView = [[UIView alloc] initWithFrame:applicationFrame];
    contentView.backgroundColor = [UIColor whiteColor];
    self.view = contentView;

    UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainBackground.jpeg"]];
    [self.view addSubview:backgroundImageView];

    CGRect tableFrame = CGRectMake(40, 40, 240, 310);
    self.listView = [[UITableView alloc] initWithFrame:tableFrame];
    self.listView.dataSource = self;
    self.listView.delegate = self;
    self.listView.scrollEnabled = NO;
    self.listView.rowHeight = 50;
    self.listView.backgroundColor = [UIColor clearColor];
    self.listView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBgMid"]];


    [self.view addSubview:self.listView];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellRow %@", indexPath.row);
    return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Did Select Row%@", indexPath.row);
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
}

Upvotes: 1

Views: 230

Answers (2)

Pareshkumar
Pareshkumar

Reputation: 176

You need to implement the tableviewdelegate and tableviewsource protocols. You will need to add an IBOutlet to UITableView that is the reference outlet in xib for the table vie and set the data source as the your UIView controller once you add the protocols.

Sorry I see your not using xib. It's curious your not getting a warning on the line where your setting the data source and delegate, so you may already have your protocols there.

Upvotes: 1

Psycho
Psycho

Reputation: 1883

Did you implement the -tableView:numberOfRowsInSection: ? Without it you won't be able to have any cells in your table view.

Upvotes: 1

Related Questions