2011-02-01 23 views
0

我有3個不同的數組。 1)ids 2)名稱3)用於emailid。從tableview中獲取選定值時的問題

我在tableview中顯示名字。

並在tableview中顯示帶勾選標記的選定名稱。

現在我需要從id和emailid數組中獲得選定的姓名ID和電子郵件。

而檢索到的id和電子郵件需要保存在兩個不同的數組中。

對我的代碼是

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.sourceArray count];; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    [cell.textLabel setText:[self.sourceArray objectAtIndex:indexPath.row]]; 

     if ([self.selectedArray containsObject:[agentemails objectAtIndex:indexPath.row]]){ 
      NSLog(@"111111111111"); 
      [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
     } 
     else 
     { 
      [cell setAccessoryType:UITableViewCellAccessoryNone]; 
     } 
     if ([self.selected_agentid_email containsObject:[agentids objectAtIndex:indexPath.row]]){ 
      NSLog(@"222222222222"); 
      [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
     } 
     else 
     { 
      [cell setAccessoryType:UITableViewCellAccessoryNone]; 
     } 
     if(myBoolean){ 
      [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
     } 



    return cell; 
} 


#pragma mark - 
#pragma mark Table view delegate 

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

     if ([self.selectedArray containsObject:[agentemails objectAtIndex:indexPath.row]]){ 
      NSLog(@"33333333333333"); 
      [self.selectedArray removeObjectAtIndex:[self.selectedArray indexOfObject:[agentemails objectAtIndex:indexPath.row]]]; 
     } 
     else 
     { 
      [self.selectedArray addObject:[agentemails objectAtIndex:indexPath.row]]; 
     } 
     if ([self.selected_agentid_email containsObject:[agentids objectAtIndex:indexPath.row]]){ 
      NSLog(@"4444444444444444"); 
      [self.selected_agentid_email removeObjectAtIndex:[self.selected_agentid_email indexOfObject:[agentids objectAtIndex:indexPath.row]]]; 
     } 
     else 
     { 
      [self.selected_agentid_email addObject:[agentids objectAtIndex:indexPath.row]]; 
     } 

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

,但我得到的問題是,

例如:如果我選擇在實現代碼如下3名,我能夠檢索只有一個EMAILID和3個IDS。

我沒有得到什麼問題是。

任何人都可以幫助我。

謝謝你提前。

回答

1

這可能不是您正在尋找的解決方案,但我會建議創建一個類來存儲這些值,因此您只需處理所有值的數組和所選值的數組。這將大大降低使用多個陣列的複雜性。我沒有真正測試下面的代碼,但這將是它想要的。

//Agent.h 
@interface Agent : NSObject 
{ 
    NSString *_name; 
    NSString *_aID; 
    NSString *_email; 
} 

@property(retain) NSString *name; 
@property(retain) NSString *aID; 
@property(retain) NSString *email; 

@end 

//Agent.m 
@implementation Agent 

@synthesize name = _name; 
@synthesize aID = _aID; 
@synthesize email = _email; 

- (void) dealloc 
{ 
    [_name release]; 
    [_aID release]; 
    [_email release]; 

    [super dealloc]; 
} 

@end 

//Your selection code will then look like this 
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    Agent *agent = (Agent*)[self.allAgents objectAtIndex:indexPath.row]; 

    if (UITableViewCellAccessoryCheckmark == cell.accessoryType) { 
     [self.selectedAgents removeObject:agent]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    else { 
     [self.selectedAgents addObject:agent]; 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

    //All 3 values are guaranteed to be there 
    NSLog(@"Agent: %@ %@ %@", agent.name, agent.aID, agent.email); 
} 
+0

感謝ü喬,但我沒有得到我在哪裏可以給代理ID和代理email.I我還未得到 - (空)的tableView:(UITableView的*)的tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {你可以用一些解釋編輯它 – MaheshBabu 2011-02-02 03:59:17