2011-12-31 52 views
0

我有一个带有复选标记公开按钮的项目表,并且我想基于哪个检查项目创建第二个表格。我知道我应该使用方法tableView:accessoryButtonTappedForRowWithIndexPath,但我不知道如何实现它。从现有表格中检查的行中创建一个新表格

所以我们假设这是我的代码,我在哪里输入ButtonTapped方法,它是如何实现的?

@implementation RootViewController 

@synthesize detailViewController; 


#pragma mark - 
#pragma mark View lifecycle 

- (void)viewDidLoad { 
    [self createFlowerData]; 
    [super viewDidLoad]; 
} 

/* 
- (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]; 
} 
*/ 

// Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 


#pragma mark - 
#pragma mark Size for popover 
// The size the view should be when presented in a popover. 
- (CGSize)contentSizeForViewInPopoverView { 
    return CGSizeMake(320.0, 600.0); 
} 


#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { 
    // Return the number of sections. 
    return [flowerSections count]; 
} 


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"CellIdentifier"; 

    // Dequeue or create a cell of the appropriate type. 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

    // Configure the cell. 
    [[cell textLabel] setText:[[[flowerData objectAtIndex:indexPath.section] objectAtIndex: indexPath.row] objectForKey:@"name"]]; 
    [[cell imageView] setImage:[UIImage imageNamed:[[[flowerData objectAtIndex:indexPath.section] objectAtIndex: indexPath.row] objectForKey:@"picture"]]]; 
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
    return cell; 
} 

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


- (void)createFlowerData { 

    NSMutableArray *redFlowers; 
    NSMutableArray *blueFlowers; 

    flowerSections=[[NSMutableArray alloc] initWithObjects: 
        @"Red Flowers",@"Blue Flowers",nil]; 

    redFlowers=[[NSMutableArray alloc] init]; 
    blueFlowers=[[NSMutableArray alloc] init]; 

    [redFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Poppy",@"name", 
          @"poppy.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Poppy",@"url",nil]]; 
    [redFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Tulip",@"name", 
          @"tulip.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Tulip",@"url",nil]]; 
    [redFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Gerbera",@"name", 
          @"gerbera.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Gerbera",@"url",nil]]; 
    [redFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Peony",@"name", 
          @"peony.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Peony",@"url",nil]]; 
    [redFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Rose",@"name", 
          @"rose.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Rose",@"url",nil]]; 
    [redFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Hollyhock",@"name", 
          @"hollyhock.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Hollyhock", 
          @"url",nil]]; 
    [redFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Straw Flower",@"name", 
          @"strawflower.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Strawflower", 
          @"url",nil]]; 

    [blueFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Hyacinth",@"name", 
          @"hyacinth.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Hyacinth_(flower)", 
          @"url",nil]]; 
    [blueFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Hydrangea",@"name", 
          @"hydrangea.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Hydrangea", 
          @"url",nil]]; 
    [blueFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Sea Holly",@"name", 
          @"seaholly.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Sea_holly", 
          @"url",nil]]; 
    [blueFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Grape Hyacinth",@"name", 
          @"grapehyacinth.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Grape_hyacinth", 
          @"url",nil]]; 
    [blueFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Phlox",@"name", 
          @"phlox.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Phlox",@"url",nil]]; 
    [blueFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Pin Cushion Flower",@"name", 
          @"pincushionflower.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Scabious", 
          @"url",nil]]; 
    [blueFlowers addObject:[[NSMutableDictionary alloc] 
          initWithObjectsAndKeys:@"Iris",@"name", 
          @"iris.png",@"picture", 
          @"http://en.wikipedia.org/wiki/Iris_(plant)", 
          @"url",nil]]; 

    flowerData=[[NSMutableArray alloc] initWithObjects: 
       redFlowers,blueFlowers,nil]; 

    [redFlowers release]; 
    [blueFlowers release]; 
} 


/* 
// 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; 
} 
*/ 


/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 


/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
} 
*/ 


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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 75.0; 
} 


#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    /* 
    When a row is selected, set the detail view controller's detail item to the item associated with the selected row. 
    */ 
    detailViewController.detailItem = 
     [[flowerData objectAtIndex:indexPath.section] 
      objectAtIndex: indexPath.row]; 

} 


#pragma mark - 
#pragma mark Memory management 

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

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

- (void)viewDidUnload { 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 


- (void)dealloc { 
    [flowerData release]; 
    [flowerSections release]; 
    [detailViewController release]; 
    [super dealloc]; 
} 

最后,我希望它是这样的图像,在项目右侧核对中出现在左边一个新表:

enter image description here

回答

0

我还没有试过,但这个应该让你用自己的方式来解决问题:

  1. 创建一个NSMutableArray的牵你的选择的行:selectedRowsArray
  2. 实施tableview:didSelectRowAtIndexPath:委托方法:
  3. 在此方法中,获取对UITableViewCell *cell = [self.tableview cellForRowAtIndexPath:indexPath]所选单元格的引用。
  4. 检查小区中已经有一个勾号,如果没有,添加复选标记,并添加你感兴趣的可变数组对象...

例:

if (cell.accessoryType != UITableViewCellAccessoryCheckmark) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    [selectedRowsArray addObject:THE_OBJECT]; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    [selectedRowsArray removeObject:THE_OBJECT]; 
} 

现在您可以使用可变数组来用数据填充另一个tableview。请记住,每次向阵列中添加或删除对象时,都要在tableview上调用reloadData

+0

哦,你可能需要将你的单元格选择样式设置为none或者一旦点击它就以编程方式取消选择该单元格。 – Rog 2011-12-31 03:54:29

相关问题