UITableView numberOfSections
- (NSInteger)numberOfSections
Return Value
The number of sections in the table view.
Discussion of [UITableView numberOfSections]
UITableView gets the value returned by this method from its data source and caches it.
UITableView numberOfSections example.
-(void) reloadViewHeight
{
float currentTotal = 0;
//Need to total each section
for (int i = 0; i < [self.tableView numberOfSections]; i++)
{
CGRect sectionRect = [self.tableView rectForSection:i];
currentTotal += sectionRect.size.height;
}
//Set the contentSizeForViewInPopover
self.contentSizeForViewInPopover = CGSizeMake(self.tableView.frame.size.width, currentTotal);
}
{
float currentTotal = 0;
//Need to total each section
for (int i = 0; i < [self.tableView numberOfSections]; i++)
{
CGRect sectionRect = [self.tableView rectForSection:i];
currentTotal += sectionRect.size.height;
}
//Set the contentSizeForViewInPopover
self.contentSizeForViewInPopover = CGSizeMake(self.tableView.frame.size.width, currentTotal);
}
Example of [UITableView numberOfSections].
- (CGSize)contentSizeForViewInPopover {
// Currently no way to obtain the width dynamically before viewWillAppear.
CGFloat width = 200.0;
CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
CGFloat height = CGRectGetMaxY(rect);
return (CGSize){width, height};
}
// Currently no way to obtain the width dynamically before viewWillAppear.
CGFloat width = 200.0;
CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
CGFloat height = CGRectGetMaxY(rect);
return (CGSize){width, height};
}
UITableView numberOfSections example.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; //one male and other female
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(section){
case 0:
return [male count];
break;
case 1:
return [female count];
break;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstLevelCell= @"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
}
SecondLevelViewController *controller;
switch([indexPath section]){
case 0:
controller = [male objectAtIndex: [indexPath row] ];
break;
case 1:
controller = [female objectAtIndex: [indexPath row] ];
break;
}
cell.textLabel.text = controller.title;
cell.imageView.image = controller.rowImage;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
{
return 2; //one male and other female
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(section){
case 0:
return [male count];
break;
case 1:
return [female count];
break;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstLevelCell= @"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
}
SecondLevelViewController *controller;
switch([indexPath section]){
case 0:
controller = [male objectAtIndex: [indexPath row] ];
break;
case 1:
controller = [female objectAtIndex: [indexPath row] ];
break;
}
cell.textLabel.text = controller.title;
cell.imageView.image = controller.rowImage;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}