Issue feeding dynamically the uitableview in the masterview controller of a master/detail app

纵饮孤独 提交于 2019-12-12 19:21:24

问题


I am trying to feed the content of a masterview after creating a project based on the master/detail app template.

I have basically a model that content all my data which might change later on. I want to feed automatically the content of the tableview by providing the number of rows per section and number of sections that has this model.

I manage to do so by overriding the following 2 functions:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

I got a NSRangeException exception though if i didn't edit the storyboard for the Master View Controller - Master Scene such that it reflects a larger number of sections and rows in sections than my model has.

e.g: I left the storyboard for the Master Scene with 1 section and 1 row. If i return 2 in the numberOfRowsInSection function my program will crash at run time with this NSRangeExceptionError error:

2011-10-10 14:38:53.042 sample_project[3636:f803] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

*** First throw call stack:

(0x13bb052 0x154cd0a 0x13a7674 0x432805 0x24227a 0x1ed548 0x1ef722 0x9f7c7 0x9f2c1 0xa228c 0xa6783 0x51301 0x13bce72 0x1d6592d 0x1d6f827 0x1cf5fa7 0x1cf7ea6 0x1d8330c 0x114c6 0x11bd6 0x20743 0x211f8 0x14aa9 0x12a5fa9 0x138f1c5 0x12f4022 0x12f290a 0x12f1db4 0x12f1ccb 0x112a7 0x12a9b 0x25e2 0x2555)

terminate called throwing an exceptionsharedlibrary apply-load-rules all

Current language:  auto; currently objective-c

(gdb) 

Does anyone see a reason for this behavior? Am I missing something? Do I need to make some extra initialization that will set a default size for the tableview in the didLoad function of the controller?


回答1:


I found out that the default table view Content attribute is set to static. You can change it to Dynamic Prototypes. You need to provide a identifier for the cell: I am not sure how this is used though. This solved the crash i was mentioning above.




回答2:


More code of yours will actually help, but anyways,

There are some changes made in UIKit in IOS5, kindly check the following link, so that you dont conflict anywhere else in the code.

IOS 5 Release Notes

There is a change in the NSIndexPath too, check that particularly. As per apple docs,

The indexPathForRow:inSection:, section, and row methods of NSIndexPath now use NSInteger instead of NSUInteger, so that these types match with methods defined on UITableView.

If you find the answer, please share.



来源:https://stackoverflow.com/questions/8088430/issue-feeding-dynamically-the-uitableview-in-the-masterview-controller-of-a-mast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!