0

我想创建包含天的动态 UITableView 作为部分(sat,sun,mon,...)

你能帮帮我吗!提前致谢!

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

天.h

#import <UIKit/UIKit.h>

@interface Day : UIViewController 

@property (nonatomic, strong) NSMutableArray *monthTitle;

@end

天.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
4

3 回答 3

14

确保将您的设置UIViewControllerdelegatedataSourceUITableView首先,您需要为这两个实现协议:

@interface Day : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *monthTitle;

@end

其次,您需要将delegateand分配dataSource给.UITableViewUIViewController

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

在此处输入图像描述

现在您UITableView已设置为使用您的UIViewControllerasdelegatedataSource。之后,正确设置函数以填充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;
}

现在,假设您已self.monthTitle设置为NSArray其中NSString包含您想要的每个部分的标题,此代码将为您提供UITableView与标题一样多的部分self.monthTitle,每个部分中有 1 行,以及与该特定相关的部分标题的指数self.monthTitle。因此,如果self.monthTitle持有一周中的所有日子,您UITableView将如下所示:

在此处输入图像描述

于 2012-07-27T13:28:40.623 回答
6

以下方法返回节数。

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

因此,如果您需要 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...
    }
于 2012-07-26T12:17:45.387 回答
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;
    }
于 2012-07-26T12:43:01.010 回答