我想创建Dynamic UITableView,它包含几天作为一个部分(sat,sun,mon,...)
你能帮帮我吗? 提前谢谢!这是我的代码,但我不知道我应该写什么来创建部分:
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
答案 0 :(得分: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...
}
答案 1 :(得分: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;
}