在objective-c中的Dynamic UITableView中创建部分

0 objective-c uitableview ios

我想创建Dynamic UITableView,它包含几天作为一个部分(sat,sun,mon,...)

你能帮帮我吗 提前致谢!

这是我的代码,但我不知道我应该写什么来创建部分:

Day.h

#import <UIKit/UIKit.h>

@interface Day : UIViewController 

@property (nonatomic, strong) NSMutableArray *monthTitle;

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Jus*_*son 14

请确保您设置UIViewController为delegate和dataSource的UITableView.首先,您需要实现这两个协议:

@interface Day : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *monthTitle;

@end
Run Code Online (Sandbox Code Playgroud)

其次,你需要分配delegate并dataSource的UITableView到UIViewController.

右键单击UITableView并将delegate和dataSource出口拖动到UIViewController视图底部的at图标:

在此输入图像描述

现在你UITableView的设置是使用你UIViewController的delegate和dataSource.之后,正确设置功能以填充UITableView数据:

-(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;
}
Run Code Online (Sandbox Code Playgroud)

现在,假设你已经self.monthTitle设置为一个NSArray的NSString保存要对每个部分的标题,这个代码将会给你一个UITableView与尽可能多的部分,因为在冠军self.monthTitle与1行中的每个部分,相关于该特定节头指数self.monthTitle.因此,如果self.monthTitle持有一周中的所有日子,您UITableView将看起来像这样:

在此输入图像描述


Ali*_*bas 6

以下方法返回节的数量.

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

所以,如果你需要3个部分,你必须返回3.

之后,您可以使用此方法命名部分:

 - (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...
    }
Run Code Online (Sandbox Code Playgroud)


小智 5

使用数组或字典创建动态结构

_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;
    }
Run Code Online (Sandbox Code Playgroud)