0

I want to create Dynamic UITableView that's contain days as a sections (sat,sun,mon,...)

would you please helping me! Thanks in advance!

here is my code but i don't know what should I write for creating sections:

Day.h

#import <UIKit/UIKit.h>

@interface Day : UIViewController 

@property (nonatomic, strong) NSMutableArray *monthTitle;

@end

Day.m

@synthesize monthTitle;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super init];
if (self) {
   monthTitle = [[NSMutableArray alloc] init];
}
return self;

}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];


}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.monthTitle count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView 
                         dequeueReusableCellWithIdentifier:@"Cell"];



return cell;

}



 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle   
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
    [self.monthTitle removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
 withRowAnimation:UITableViewRowAnimationFade];
}   
}




#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib   
name#>" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 */
}

@end
1
  • Where is your UITableView? Did you build it in interface builder? Commented Jul 26, 2012 at 12:36

3 Answers 3

14

Make sure that you set up your UIViewController as the delegate and dataSource of the UITableView. First you need to implement the protocols for these two:

@interface Day : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *monthTitle;

@end

Second you need to assign the delegate and dataSource of the UITableView to the UIViewController.

Right click on the UITableView and drag the delegate and dataSource outlets to the UIViewController at icon at the bottom of the view:

enter image description here

Now your UITableView is set up to use your UIViewController as the delegate and dataSource. After that, set up the functions properly to fill the UITableView with data:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.monthTitle count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.monthTitle objectAtIndex:section];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;  //Return the number of sections you want in each row
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"Whatever You Put For Cell Identifier On Interface Builder";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    //Configure the cell however you wish

    return cell;
}

Now, assuming you have self.monthTitle set up as an NSArray of NSString that holds the titles you want for each section, this code will give you a UITableView with as many sections as there are titles in self.monthTitle with 1 row in each section and a section header that correlates to that particular index of self.monthTitle. So if self.monthTitle holds all of the days of the week, your UITableView will look like this:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

6

The following method returns the number of sections.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

So, if you need 3 sections, you have to return 3.

After that, you can use this method to name sections :

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

        if(section == 0)
            return @"name section 0";
        else if(section == 1){
            return @"name section 1";
        }else
            return @"name section 2";
         //etc...
    }

Comments

5

Use Array or dictionary to create dynamic structure

_sections = [NSMutableArray array];
            [_sections addObject:@"section1"];
            [_sections addObject:@"section2"];

    _rows = [NSMutableArray array];
    NSMutableArray* section1 = [NSMutableArray array];
    [section1 addObject:@"row1"];
    [section1 addObject:@"row2"];
    NSMutableArray* section2 = [NSMutableArray array];
    [section1 addObject:@"row1"];
    [section1 addObject:@"row2"];
    [_rows addObject:section1];
    [_rows addObject:section2];

    #pragma mark - Table view data source

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [_sections count];
    }

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return [_sections objectAtIndex:section];
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [[_rows objectAtIndex:section] count];
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:nil];
        if(!cell)
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        cell.textLabel.text = [[_rows objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
        return cell;
    }

2 Comments

@Nomad where should I put _sections and _row staff because I will get an error
Add to the declaration of class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.