user4441082 user4441082 - 3 months ago 11
iOS Question

TableView not showing more than 10 rows

I am making a tableview, when showing 1-10 rows, everything works fine. However, When adding more than 10 rows. It is building, but in the simulator, it only shows 10 rows.

Here is my code:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.greekLetters=
@[@"CHEM 1100",@"CHEM 2100 ",@"CHEM 3415W",@"CHEM 3511",@"CHEM 3521",@"CHEM 4600",@"PHYS 1150",@"PHYS 2150",@"MATH 1201",@"MATH 1206",@"Adv courses",@"CHEM 2700"];

}

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

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleIdentifier];

if (cell ==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SimpleIdentifier];
}
cell.textLabel.text=self.greekLetters[indexPath.row];

if(indexPath.row==0) cell.detailTextLabel.text=@"General Chemistry I";
if(indexPath.row==1) cell.detailTextLabel.text=@"General Chemistry II";
if(indexPath.row==2) cell.detailTextLabel.text=@"Analytical Chemistry";
if(indexPath.row==3) cell.detailTextLabel.text=@"Organic Chemistry I";
if(indexPath.row==4) cell.detailTextLabel.text=@"Organic Chemistry II";
if(indexPath.row==5) cell.detailTextLabel.text=@"Physical Chemistry(HP)";
if(indexPath.row==6) cell.detailTextLabel.text=@"General Physics I";
if(indexPath.row==7) cell.detailTextLabel.text=@"General Physics II";
if(indexPath.row==8) cell.detailTextLabel.text=@"Calculus I";
if(indexPath.row==9) cell.detailTextLabel.text=@"Calculus II";
if(indexPath.row==10) cell.detailTextLabel.text=@"pick 5 creit";
if(indexPath.row==11) cell.detailTextLabel.text=@"Introduction to Inorganic Chemistry";

return cell;

}

-(NSIndexPath *) tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{

return indexPath;

}


Is the problem of my code or the simulator please? Is it because when I put tableview on the stroyboard, something not right there please?

Rob Rob
Answer

There are one of two problems. Either there is a problem with the UITableViewDataSource methods (which does not appear to be the case), or the table view, itself, is not the right size.

For this second scenario (where the table view has been put on a view controller's view, but has the wrong size), there are two ways to diagnose this. The old-school technique is to run the program in the debugger, pause execution, and enter po [[UIWindow keyWindow] recursiveDescription] at the (lldb) prompt:

(lldb) po [[UIWindow keyWindow] recursiveDescription]
<UIWindow: 0x7b7956b0; frame = (0 0; 320 480); gestureRecognizers = <NSArray: 0x7b795b30>; layer = <UIWindowLayer: 0x7b795670>>
   | <UIView: 0x7c943380; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x7c9434f0>>
   |    | <UITableView: 0x7d064a00; frame = (0 0; 600 600); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7c941de0>; layer = <CALayer: 0x7c93d3f0>; contentOffset: {0, 0}; contentSize: {600, 528}>
   |    |    | <UITableViewWrapperView: 0x7c942830; frame = (0 0; 600 600); gestureRecognizers = <NSArray: 0x7c942df0>; layer = <CALayer: 0x7c942b30>; contentOffset: {0, 0}; contentSize: {600, 600}>
   |    |    |    | <UITableViewCell: 0x7c955b50; frame = (0 484; 600 44); text = 'CHEM 2700'; autoresize = W; layer = <CALayer: 0x7c955ce0>>
   |    |    |    |    | <UITableViewCellContentView: 0x7c955d30; frame = (0 0; 600 43.5); gestureRecognizers = <NSArray: 0x7c955f40>; layer = <CALayer: 0x7c955da0>>
   |    |    |    |    |    | <UITableViewLabel: 0x7c955f90; frame = (15 12; 93 20.5); text = 'CHEM 2700'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7c956050>>
   |    |    |    |    |    | <UITableViewLabel: 0x7c9562c0; frame = (314.5 12; 270.5 20.5); text = 'Introduction to Inorganic...'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7c956380>>
   |    |    |    |    | <_UITableViewCellSeparatorView: 0x7c956680; frame = (15 43.5; 585 0.5); layer = <CALayer: 0x7c956700>>
   |    |    |    | ...

If you look at the above example, the main UIView has a size of 320x480, but the tableview curiously has a size of 600x600. That's not good. They need to be the same size if you want the table view to scroll properly.

The other way to diagnose this in Xcode 6 is to run the app in the debugger and then you can pause the execution and then press the debug view hierarchy button:

debug view hierarchy

If you do that, you'll see the view hierarchy and if there are any views that are the wrong size, they'll stand out like this:

view hierarchy debugging screen

In terms of fixing this, the typical solution is to make sure you have all of the constraints for your table view (e.g. here I selected the table view and I'm adding leading, trailing, top and bottom constraints):

add constraints

Then, when I rerun the app and look at the view hierarchy, everything matches up nicely:

(lldb) po [[UIWindow keyWindow] recursiveDescription]
<UIWindow: 0x7bc34ee0; frame = (0 0; 320 480); gestureRecognizers = <NSArray: 0x7bc352f0>; layer = <UIWindowLayer: 0x7bc32680>>
   | <UIView: 0x7ba387d0; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x7ba38940>>
   |    | <UITableView: 0x7d08d400; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ba37260>; layer = <CALayer: 0x7ba32a80>; contentOffset: {0, 0}; contentSize: {320, 528}>
   |    |    | <UITableViewWrapperView: 0x7ba37ca0; frame = (0 0; 320 480); gestureRecognizers = <NSArray: 0x7ba38260>; layer = <CALayer: 0x7ba37fa0>; contentOffset: {0, 0}; contentSize: {320, 480}>
   |    |    |    | <UITableViewCell: 0x7ba3f270; frame = (0 0; 320 44); text = 'CHEM 1100'; autoresize = W; layer = <CALayer: 0x7ba3f5a0>>
   |    |    |    |    | <UITableViewCellContentView: 0x7ba3faf0; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x7ba40400>; layer = <CALayer: 0x7ba3fcc0>>
   |    |    |    |    |    | <UITableViewLabel: 0x7ba40840; frame = (15 12; 93 20.5); text = 'CHEM 1100'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ba40b20>>
   |    |    |    |    |    | <UITableViewLabel: 0x7ba44a10; frame = (153.5 12; 151.5 20.5); text = 'General Chemistry I'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ba44ad0>>
   |    |    |    |    | <_UITableViewCellSeparatorView: 0x7ba456a0; frame = (15 43.5; 305 0.5); layer = <CALayer: 0x7ba45840>>
   |    |    |    | ...