How can I start a UITableView section with number 0 when I have multiple sections?

=> I know this is a stupid question, but it is more important for a new iPhone developer like me.

here I have a question related to the UITableView section :

First problem: I have a question: how can I start a UITableView section with number 0 from several sections?

Example:

I have 11 sections in a UITableView and in the delegate method numberOfSectionsInTableView , my section starts with the 11th number of sections, then 0,1,2,3,4,5,6,7,8, 9 and 10. since I can run the partition number with the number 0, and then as it is.

second problem: in the titleForHeaderInSection delegate method, all sections of the View table are repeated 3 times. and with the current ViewController i, scrolling UITableView up or down, then the section displays as normal (0 to 11) this normal section of the UITableView display after repeating the section 3 times.

Example:

I am using NSLog (@" %d ", section ); in the titleForHeaderInSection delegate method, and then print to the console as shown below

Console:

 2012-09-17 12:27:47.424 Quotes2You[1623:f803] 11 2012-09-17 12:27:47.426 Quotes2You[1623:f803] 11 2012-09-17 12:27:47.427 Quotes2You[1623:f803] 11 2012-09-17 12:27:47.428 Quotes2You[1623:f803] 0 2012-09-17 12:27:47.429 Quotes2You[1623:f803] 0 2012-09-17 12:27:47.429 Quotes2You[1623:f803] 0 2012-09-17 12:27:47.430 Quotes2You[1623:f803] 1 2012-09-17 12:27:47.431 Quotes2You[1623:f803] 1 2012-09-17 12:27:47.431 Quotes2You[1623:f803] 1 2012-09-17 12:27:47.432 Quotes2You[1623:f803] 2 2012-09-17 12:27:47.433 Quotes2You[1623:f803] 2 2012-09-17 12:27:47.433 Quotes2You[1623:f803] 2 2012-09-17 12:27:47.434 Quotes2You[1623:f803] 3 2012-09-17 12:27:47.435 Quotes2You[1623:f803] 3 2012-09-17 12:27:47.436 Quotes2You[1623:f803] 3 2012-09-17 12:27:47.436 Quotes2You[1623:f803] 4 2012-09-17 12:27:47.437 Quotes2You[1623:f803] 4 2012-09-17 12:27:47.438 Quotes2You[1623:f803] 4 2012-09-17 12:27:47.438 Quotes2You[1623:f803] 5 2012-09-17 12:27:47.439 Quotes2You[1623:f803] 5 2012-09-17 12:27:47.439 Quotes2You[1623:f803] 5 2012-09-17 12:27:47.440 Quotes2You[1623:f803] 6 2012-09-17 12:27:47.441 Quotes2You[1623:f803] 6 2012-09-17 12:27:47.462 Quotes2You[1623:f803] 6 2012-09-17 12:27:47.463 Quotes2You[1623:f803] 7 2012-09-17 12:27:47.464 Quotes2You[1623:f803] 7 2012-09-17 12:27:47.465 Quotes2You[1623:f803] 7 2012-09-17 12:27:47.466 Quotes2You[1623:f803] 8 2012-09-17 12:27:47.466 Quotes2You[1623:f803] 8 2012-09-17 12:27:47.467 Quotes2You[1623:f803] 8 2012-09-17 12:27:47.472 Quotes2You[1623:f803] 9 2012-09-17 12:27:47.476 Quotes2You[1623:f803] 9 2012-09-17 12:27:47.478 Quotes2You[1623:f803] 9 2012-09-17 12:27:47.480 Quotes2You[1623:f803] 10 2012-09-17 12:27:47.481 Quotes2You[1623:f803] 10 2012-09-17 12:27:47.481 Quotes2You[1623:f803] 10 2012-09-17 12:27:47.487 Quotes2You[1623:f803] 0 2012-09-17 12:27:47.487 Quotes2You[1623:f803] 1 2012-09-17 12:27:47.489 Quotes2You[1623:f803] 2 

first repeat all sections 3 times and after I scroll through a UITableView, then the start starts at 0 (as usual)

My code is here:

ThirdViewController.h

 @interface ThirdViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> { UITableView *tblCustomer; NSArray *listOfsection; } @property (nonatomic,retain) UITableView *tblCustomer; @property (nonatomic,retain) NSArray *listOfsection; @end 

ThirdViewController.m

 @synthesize tblCustomer , listOfsection; - (void)viewDidLoad { [super viewDidLoad]; self.tblCustomer = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,417) style:UITableViewStyleGrouped]; self.tblCustomer.delegate = self; self.tblCustomer.dataSource = self; [self.view addSubview:self.tblCustomer]; self.listOfsection = [[NSArray alloc] initWithObjects:@"Name", @"Company", @"Address", @"Email", @"Mobile", @"Phone", @"Potential", @"Sales Status", @"Genre", @"Distributor", @"Dist Rep", @"Internal Notes", nil]; } #pragma mark - UITableView Datasource Methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { return [self.listOfsection count]; } - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { int numSection=0; if (section == 2) numSection = 5; else numSection = 1; return numSection; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // here is my code cellForRowAtIndexPath as section wise } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSLog(@" %d ", section); NSString *sectionHeader = nil; sectionHeader = [self.listOfsection objectAtIndex:section]; // here is section overwrite // return sectionHeader; } #pragma mark - Memory Management -(void)dealloc { [super dealloc]; [self.listOfsection release]; [self.tblCustomer release]; } 

Thank you in advance

+4
source share
3 answers

You are asking:

First problem: I have a question: how can I start a UITableView section with number 0 from several sections?

I assume that your horror stems from the fact that they are not invoked in a strictly sequential manner. This is how this happens, and you cannot change this behavior. Obviously, your sections are displayed in the correct order in the application, but they just don't get called sequentially. It is simply the nature of event driven programming. But I donโ€™t see anything in your code that breaks down on the basis of this order of execution, but maybe something in your code that you did not include here that requires a strictly sequential call to these methods, and if so, you will have to reorganize this code. But I don't see anything like it in the above code snippets. The nature of the UITableViewDataSource delegate methods means that you can (and should) write your methods in such a way that they are independent of the order in which cells or sections are called. Never rely on a sequence of events from the user interface to control how you build your model that underlies the view.

The second problem: in the delegate method titleForHeaderInSection, all sections of the View table are repeated 3 times. and with the current ViewController i, scrolling UITableView up or down, then the section displays as normal (0 to 11) this normal section of the UITableView display after repeating the section 3 times.

I would be surprised if he was called three times in a row. I would bet (especially if your journal shows evidence of slightly different formatted NSLog statements) that there are other NSLog statements in your code. I will try to change your NSLog statement to something like:

 NSLog(@"%s section=%d", __FUNCTION__, section); 

Thus, (a) you know from which method it is registered; and (b) by adding the descriptive string "section=" to the format string, this means that if you will never be confused about which numbers are being recorded. I bet that not all of these three NSLog statements apply to your titleForHeaderInSection . Personally, I no longer put NSLog expressions in my code without the __FUNCTION__ link, since it is too easy to confuse what the NSLog operator generates.

But even if titleForHeaderInSection is called multiple times, so what? iOS has been optimized behind the scenes to do all kinds of things. Itโ€™s good for us to understand what is happening (for example, to make sure that you arenโ€™t doing something stupid, like some kind of intense or intensive network work in titleForHeaderInSection ), but this is the time to embrace the prayer of serenity and appreciate what we, like developers, we control, but what we donโ€™t have. (And the order and number of calls to the UITableViewDataSource is one of those things that we donโ€™t control.) As long as you are not a source of redundant method calls (for example, making unnecessary reloadData calls), I donโ€™t worry about that.

To summarize, while I would be surprised if titleForHeaderInSection is called three times in a row, don't rely on it being called only once. I think that table views can call it once for each section of the table (perhaps to do something like determine the height of the whole table so that the size of the scroll bar matches the size) and then again for the sections that are visible on the screen (to actually display section headings).

+2
source
 -(NSInteger)numberOfRowsInTotal { NSInteger sections = self.numberOfSections; NSInteger cellCount = 0; for (NSInteger i = 0; i < sections; i++) { cellCount += [self numberOfRowsInSection:i]; } return cellCount; } 
0
source

This is not the right way, but you can use the code below and it works perfectly.

 [self reloadDataWithCompletion:^{ communityFoundIndex = [[NSMutableArray alloc]init]; NSRange range = NSMakeRange(0, 1); NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range]; [self.myTbl reloadSections:section withRowAnimation:UITableViewRowAnimationNone]; }]; - (void) reloadDataWithCompletion:( void (^) (void) )completionBlock { [_tblCommunity reloadData]; if(completionBlock) { completionBlock(); } } 
0
source

Source: https://habr.com/ru/post/1434364/


All Articles