Question

I've ran into a problem. I have a TableView that I have created programmatically which does not display the TextView that I have added corresponding to the rows of TableView. I cannot find a solution to this on Stack Overflow.

#import "MasterTableViewController.h"
@interface MasterTableViewController ()
@end
@implementation MasterTableViewController

-(NSMutableArray *)numbers {
    if(!_numbers)
    {
        _numbers = [[NSMutableArray alloc] init];
    }
    return _numbers;
}

-(NSMutableArray *)subtile {
    if(!_subtile)
    {
        _subtile = [[NSMutableArray alloc] init];
    }
    return _subtile;
}

- (void)viewDidLoad
{
    //Adding Titles
    [self.champions addObject:@"1"];
    [self.champions addObject:@"2"];

    //Adding subtitles

    [self.subtileChamps addObject:@"One"];
    [self.subtileChamps addObject:@"Two"];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.numbers.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    cell.textLabel.text = self.numbers[indexPath.row];
    cell.detailTextLabel.text = self.subtile[indexPath.row];

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
@end
Was it helpful?

Solution

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle: reuseIdentifier:]
}

You should init your cell

And... You have a common way for initiate your variables. This one should be suitable for you. That method calls before all others and you can initiate your variables here:

-(id)initWithStyle:(UITableViewStyle)style
{
    if (self = [super initWithStyle:style])
    {
        someArray = [NSMutableArray array]; //init the array
    }

    return self;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top