从tableview获取选定值时出现问题

时间:2011-02-01 14:35:13

标签: iphone

我有3个不同的阵列。 1)for id 2)for name 3)for emailid。

我在桌面视图中显示名称。

在tableview中显示带有复选标记的所选名称。

现在我需要从ids和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];
}

但我得到的问题是,

例如:如果我在tableview中选择3个名称,我只能检索一个emailid和3个ID。

我没有得到什么问题。

任何人都可以帮助我。

提前感谢你。

1 个答案:

答案 0 :(得分: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);
}