Search code examples
iosuitableviewcustom-cell

UITableView datasource issue


I'm trying to make a basic custom cell and after I followed the tutorials I'm getting this issue.

reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

- (NSInteger)numberOfSections;
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array1 count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MainCell";

CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

/*if (! customCell) {
    NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"cell" owner:nil options:nil];
    customCell = [parts objectAtIndex:0];
}
*/

customCell.lblTime.text = [array1 objectAtIndex:indexPath.row];
customCell.lblEvent.text = [array2 objectAtIndex:indexPath.row];

return customCell;
}

Also I have set the Identity on the tableView but still it is not working and I'm using Storyboard. Moreover I have did linked the datasource and delegate. Please where would be my issue?


Solution

  • In the first execution the cell is nil . So write code to initialize the cell.

    -(UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath:  
       (NSIndexPath *)indexPath{
    
        static NSString *CellIdentifier = @"MainCell";
    
        CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
        if (cell == nil) {
           NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
           cell = [parts objectAtIndex:0];
        }
    
        cell.lblTime.text = [array1 objectAtIndex:indexPath.row];
        cell.lblEvent.text = [array2 objectAtIndex:indexPath.row];
    
        return cell;
    }