0

The cell in my tableview just won't display any content.

- (void)viewDidLoad {
[super viewDidLoad];
testArray = [[NSMutableArray alloc] init];    
}

If the button is clicked, the app will go to next screen and display the table. (The log shows that testString is stored into the testArray)

- (IBAction)goNext:(id)sender {

   for(int i=0; i<10; i++){           
             NSString *testString = @"Hello";
             [testArray addObject:testString];
             NSLog(@"myArray:\n%@", testArray[i]);        
         }
         UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
         UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"test"];
         [self presentViewController:vc animated:YES completion:nil];

}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

cell.textLabel.text = testArray[indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int)indexPath.row + 1];

return cell;

}

If I do it this way, the content will display.

- (void)viewDidLoad {
[super viewDidLoad];
[self testArraySetup];    
}

- (void)testArraySetup{

testArray = [NSMutableArray arrayWithArray:@[@"Pizza",@"Bread",@"Drinks"]];

}

Any advice is much appreciated.

bubibu
  • 324
  • 1
  • 4
  • 12

3 Answers3

2

If you want to pass test Array of one Controller A to another UIViewController B on clicking the Next button you should make the property of testArray in UIViewController B in which you want to show your tableView.

    // UIVIewController A
@interface ViewControllerA ()
{
   NSMutableArray * testArray;
}
    - (void)viewDidLoad {
    [super viewDidLoad];
    testArray = [[NSMutableArray alloc] init];    
    }

        - (IBAction)goNext:(id)sender {

           for(int i=0; i<10; i++){           
                     NSString *testString = @"Hello";
                     [testArray addObject:testString];
                     NSLog(@"myArray:\n%@", testArray[i]);        
                 }
                 UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                 ViewControllerB *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"test"];
                 vc.testArray = testArray;
                 [self presentViewController:vc animated:YES completion:nil];

        }

    // ViewController B
    @interface ViewControllerB : UIViewController
        @property (nonatomic,retain)NSMutableArray * testArray;

No need to reinitialise testArray in viewDidLoad of UIViewController B.Otherwise it will reallocate memory to testArray.

Daggarwal
  • 234
  • 1
  • 7
  • Got an error: Property testArray not found on object of type 'UIViewContoller' – bubibu Nov 08 '16 at 05:35
  • Create this property in to the interface of UIViewController Class in which you implemented tableView delegates. – Daggarwal Nov 08 '16 at 06:54
  • vc object should be a instance of ViewControllerB class not UIViewController like this "ViewControllerB *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"test"];" – Daggarwal Nov 08 '16 at 07:11
  • Thanks your solution works like a charm! The problem is I did not pass the array to the viewController B properly so content wasn't displaying anything. Thanks for your help! – bubibu Nov 08 '16 at 07:24
1

This line...

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

...does not create a cell and would return nil if there are not already cells in the reuse queue (which is the initial state when the app is first run and you've not explicitly created any cells yet). This means that your entire method would return nil (ie, no cell).

When using this, you also need to check for cell == nil and if so, then you need to create a new cell explicitly yourself with something like:

if ( ! cell )
   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

Alternatively, you can register a class for the identifier and then use [tableView dequeueReusableCellWithIdentifier: forIndexPath:] instead, which will generate a cell for you based on the class that you registered.

For information on the difference between the two, see: When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier : forIndexPath

Community
  • 1
  • 1
Son of a Beach
  • 1,733
  • 1
  • 11
  • 29
  • Please post your updated code. My solution is likely only one part of the problem (as described in other answers). It certainly cannot work if you do not create any cells! – Son of a Beach Nov 08 '16 at 06:54
1

Possible issues with solutions:

1: You have not set datasource and delegate for UITableView to self properly.

solution: add these lines in your ViewDidLoad OR ViewDidAppear

yourTableView.datasource = self;
yourTableView.delegate = self;

2: You have not make a proper IBOutlet connection for your TableView in the Storyboard.

solution: create @property (nonatomic, weak) IBOutlet UITableView *tableView; and now go to storyboard, under connection inspector you can connect your property with tableView in storyboard by control drag to it.

3: From your code, it seems like you are using datasource and delegate methods for TableView in your ViewControllerA and you are trying to show the contents in ViewControllerB.

Solution: You should implement UITableView's datasource and delegate in that respective ViewControllerB class.

4: you are not properly initializing / reusing the UITableViewCell.

Solution: see the answer of @Son of A Beach

M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45