I’m working on an app where I need to show 5 rows of data all the time. The data for the rows comes from a NSArray of strings. The NSArray can be empty or can contain a max of 5 elements. I wanted to force the UITableView to have 5 rows displayed all the time. I tried it and when the NSArray has just 1 element, iOS throws a exception due to the fact I have defined 5 rows (numberOfRowsInSection). How do I make the UITableView to display 5 rows (Grouped, single line) all the time without raising a exception?.
Also, how do I center the section header? I’m using titleForHeaderInSection method.
I’m working on an app where I need to show 5 rows of data
Share
In your
cellForRowAtIndexPath, you need to implement logic that’s based on the size of yourNSArrayof strings.Find the total number of strings using
[NSArray count]. Then, check ifindexPath.rowis less than[NSArray count]and use your standard implementation where you get the string from the array usingindexPath.rowas your index. IfindexPath.rowis not less than[NSArray count], then you can just keep the cell empty (and not try to retrieve anything from your array). Then you won’t have that exception.For the title header, does this work?
https://discussions.apple.com/thread/2539133?start=0&tstart=0
Replace
UITextAlignmentCenterwithNSTextAlignmentCenter.It works around your issue by using
viewForHeaderInSectionand returning a centeredUILabel.