How can i start my UITableView section with number 0 when i have multiple sections?

=> I know , this is silly question , but this is more important for new iPhone developer as I.

here i have question related to UITableView section :

  • Change Face Color From main color in xcode
  • Align text fields using dynamic constraints
  • How to log into web site using uiwebview with swift?
  • CMake and XCode: “cannot find interface declaration for 'NSObject'”
  • Xcode 6 - Unknown type name 'NSString' / Expected identifier or '('
  • How to get the list of friend without opening FBFriendPickerViewController iOS
  • First Problem : i have question that how can i start my UITableView section with number 0 from multiple section ?

    For Example :

    I have 11 number of section in UITableView and in numberOfSectionsInTableView delegate method, my section is first start with 11th number of section then 0,1,2,3,4,5,6,7,8,9 and 10.
    so how can i start my section number with 0 number and then as it is .

    second problem : in titleForHeaderInSection delegate method my all sections of tableView is repeat 3 times. and at current ViewController i scrolling UITableView up or down then section display as normal (0 to 11 ) this normal section of UITableView display after repetition of section 3 times.

    For Example :

    I use NSLog (@" %d ", section ); in titleForHeaderInSection delegate method then display out put in console as as below

    Console out put :

    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 this repeat all section 3 times and after when i scroll UITableView then section start with 0 (as normal)

    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];
    }
    

    Thanks in advance

    3 Solutions Collect From Internet About “How can i start my UITableView section with number 0 when i have multiple sections?”

    You ask:

    First Problem : i have question that how can i start my UITableView section with number 0 from multiple section ?

    I assume your consternation stems from the fact that these aren’t invoked in strictly sequential fashion. That’s just how it happens to work and you can’t change this behavior. Clearly, your sections appear in the right order in the app, but they just don’t happen to be invoked sequentially. That’s just the nature of event-driven programming. But, I see nothing in your code that breaks on the basis of this order of execution, but perhaps there’s something in your code that you haven’t included here that requires a strictly sequential invocation of those methods, and if so, you’ll have to refactor that code. But I’m seeing nothing like that in the above code snippets. The nature of the UITableViewDataSource delegate methods mean that you can (and should) write your methods in such a manner that they’re not dependent upon the order that the cells or sections are invoked. Never rely on the sequence of events from the UI to govern how you build your view’s underlying model.

    second problem : in titleForHeaderInSection delegate method my all sections of tableView is repeat 3 times. and at current ViewController i scrolling UITableView up or down then section display as normal (0 to 11 ) this normal section of UITableView display after repetition of section 3 times.

    I’d be surprised if it’s being called three times in succession. I’d wager (especially given your log shows evidence of slightly different formatted NSLog statements) that you have other NSLog statements in your code somewhere. I’d try changing your NSLog statement to something like:

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

    This way, (a) you know which method it’s being logged from; and (b) by adding the descriptive "section=" in the formatting string, it means that if you will never be confused about what numbers are being logged. I’d be willing to bet that not all of these three NSLog statements are from your titleForHeaderInSection. Personally, I no longer put NSLog statements in my code without the __FUNCTION__ reference, as it’s just too easy to get confused as to what’s generating what NSLog statement.

    But even if titleForHeaderInSection is called multiple times, so what? iOS has been optimized behind the scenes to do all sorts of stuff. It’s good for us to understand what’s happening (e.g. to make sure you don’t do something silly like some computationally intensive or network intensive activity in titleForHeaderInSection), but this is a time to embrace the serenity prayer and appreciate what we as developers have control over, and that which we don’t. (And the order and number of the UITableViewDataSource calls is one of those things we don’t control.) As long as you’re not the source of redundant calls to the methods (e.g. doing unnecessary reloadData calls), I wouldn’t worry about it.

    To sum up, while I’d be surprised if titleForHeaderInSection is being called three times consecutively, don’t rely on it being called only once. I think table views may call it once for each section of the table (probably to do something like figure out the height of the whole table so the scroll bar will be sized appropriately) and then once again for the sections that are visible on the screen (to actually show the section headers).

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

    This not proper way but you can use below code and its working perfect.

    [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();
        }
    }