3

我想在uitableview中有7个不同行的部分,我不知道我应该如何编写不同的数组并将其设置在不同的部分中,我只写一个,但每个部分都有相同的数组!

你能帮帮我吗!

提前致谢!

我的意思是像下面的图片,但使用不同的数组:例如,我不想在每个部分都有测试和嘿

这是我的代码:

@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];
 arry = [[NSMutableArray alloc] init];
[arry addObject:@"test"];
[arry addObject:@"hey"];



// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
 target:self action:@selector(addNewItem)];
self.navigationItem.rightBarButtonItem = rightButton;
}

-(void)addNewItem{
[arry addObject:@"New Day"];
    [self.tableView reloadData];
}
//- (IBAction)DeleteButtonAction:(id)sender
//
//{
//    
//    [arry removeLastObject];
//    
//    [self.tableView reloadData];
//    
//}

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

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (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 7;
//_week.days.count;
}

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

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


cell.textLabel.text = [arry objectAtIndex:indexPath.row];
return cell;

}


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

if(section == 0)
    return @"Monday";
else if(section == 1){
    return @"Tuesday";
}else if(section == 2){
    return @"Wednesday";
} else if(section == 3){
    return @"Thuesday";
} else if(section == 4){
    return @"Friday";
} else if(section == 5){
    return @"Saturday";
}else
    return @"Sunday";

}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}




- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
  forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)  
{
    [arry removeObjectAtIndex:indexPath.row];
    //[self.monthTitle removeObjectAtIndex:indexPath.row];

    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:
     UITableViewRowAnimationFade];

//    }else if (editingStyle==UITableViewCellEditingStyleInsert) {

} 

// [self.tableView reloadData]; }

#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

2 回答 2

2

我认为以下文档中的数组部分将指导您: http ://briterideas.blogspot.com/2012/07/uitableview-how-to-part-3-multiple.html

于 2012-07-27T14:29:20.310 回答
2

在 cellForRowAtIndexPath 函数中,放置这样的条件。

if(indexpath.section == 0)
{
   cell.textlabel.text = [array1 objectAtIndex:indexpath.row];
}
else if(indexpath.section == 1)
{
   cell.textlabel.text = [array2 objectAtIndex:indexpath.row];
}
..
..
..
etc
于 2012-07-27T14:33:55.240 回答