Search code examples
iosobjective-ctableviewcelltitle

TableViewCell Title not showing


My UITableCell title in my viewController is not showing.

What's wrong with it?

I've dragged the TableView and TableViewCell on it but it doesn't show, it stays white.

Screenshot: https://blazor.nl/uploads/get/19d2ed8a2662039e1104474b0426f2e0/IMG-0286

I have used below code

-(void) arraySetup{
    imageNameArray = [NSMutableArray arrayWithArray:@[@"image1", @"image2", @"image3", @"image4"]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return imageNameArray.count;
}

#pragma mark - UITableView DataSource Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    return cell;
}

Please advice me how to do it?


Solution

  • That's obvious, because you're dequeuing a table view cell (that's, basically, a template), but you're not filling it with data. So:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *cellId = @"cell";
    
        // Fix: Use dequeueReusableCellWithIdentifier:forIndexPath: instead
        // of dequeueReusableCellWithIdentifier: alone. The new method
        // ensures you won't get a nil cell and, thus, crashing your app.
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
                                                                forIndexPath:indexPath];
    
        // Grab your data here
        NSString *theTitle = imageNameArray[indexPath.row];
    
        // And set the title of your cell
        cell.textLabel.text = theTitle;
    
        return cell;
    }