Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView delegate and dataSource methods not getting called

I have a UIView which contains a UITableView. The tableview's delegate is set to my UIView, but it never calls the delegate methods:

-(id)init {
    self = [super init];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:self.bounds];
        self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.scrollEnabled = NO;
        self.tableView.layer.cornerRadius = PanelCornerRadius;
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - UITableViewDelegate methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"height for row");
    int height = [self heightForRowAtIndexPath:indexPath];

    return height;
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"number of rows");
    if (self.manager.fetchOperation.dataFetchProblem) {
        return 1;
    }

    int numberOfRows = [self.delegate numberOfSectionsInPanel:self];

    return numberOfRows;
}

I've explored every option I can think of, but can't seem to find the root of the problem.

EDIT: Included the numberOfRowsInSection. It could potentially return 0, only it never gets that chance because the "number of rows" NSLog never gets called.

like image 811
Andrew Avatar asked Sep 23 '13 22:09

Andrew


People also ask

What are delegate and datasource methods of UITableView?

Datasource methods are used to generate tableView cells,header and footer before they are displaying.. Delegate methods provide information about these cells, header and footer along with other user action handlers like cell selection and edit..

What is tableView delegate?

func tableView(UITableView, willDisplay: UITableViewCell, forRowAt: IndexPath) Tells the delegate the table view is about to draw a cell for a particular row. func tableView(UITableView, indentationLevelForRowAt: IndexPath) -> Int. Asks the delegate to return the level of indentation for a row in a given section.

What is tableView data source?

Overview. Table views manage only the presentation of their data; they don't manage the data itself. To manage the data, you provide the table with a data source object — an object that implements the UITableViewDataSource protocol. A data source object responds to data-related requests from the table.

What is a uitableviewdatasource/delegate?

What is a UITableViewDatasource/Delegate? The UITableViewDatasource has a few methods that help you manage data and provide the cells for your table view. The general methods that gets used from the datasource in most tutorials would include the numberOfRowsInSection and the cellForRowAt indexPath.

What is the difference between a delegate and a DataSource?

The delegate would require a delegate property that it would be initiazed with, and the datasource would require the data to be passed through when it was being initialized. With regards to the datasource, this could be the place where you make network calls to get your data etc.

How to display the correct uitableviewcell for a row in Table View?

The cellForRowAt indexPath method provides the correct UITableViewCell for a row in the table view. You will need to use the datasource methods to manage and display the data you want to display in the table view. To do this you need to get the correct data for the cell that you are busy setting up.

What methods are required from the data source in a Tableview?

The following methods are required from the data source: tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath:. If your table is a grouped table, you must also implement numberOfSectionsInTableView:.


2 Answers

Can you please write the code that you have written on your cellForRowAtIndexPath: method? Because i can't find anything wrong with your code.

Are you calling your UIView from some other ViewController? If yes, how?

I have done something like this once. I declared a method in the UIView like this :

- (void)setUpTableView{
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
}

And then i called the setUpTableView from the view controller i added this view to, like this :

[self.yourView setUpTableView]; 

This might help. Thanks.

like image 156
try catch finally Avatar answered Sep 28 '22 07:09

try catch finally


I had a similar problem, my solution was because I did not set the number of sections to 1.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
like image 40
Darkin Avatar answered Sep 28 '22 07:09

Darkin