如何检查tableview中的所有名称

时间:2011-01-28 10:52:46

标签: iphone cocoa-touch

我在tableview中显示一个名字的数组。

选中的行将带有支票表示,并且很好地我将选定的名称添加到列表中。

我的代码是

- (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]]){
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    else
    {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }

    return cell;
}


#pragma mark -
#pragma mark Table view delegate

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

    if ([self.selectedArray containsObject:[agentemails objectAtIndex:indexPath.row]]){
        [self.selectedArray removeObjectAtIndex:[self.selectedArray indexOfObject:[agentemails objectAtIndex:indexPath.row]]];
    }
    else
    {
        [self.selectedArray addObject:[agentemails objectAtIndex:indexPath.row]];
    }

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

现在我需要放置一个按钮select-all,当我使用点击它时,我需要检查表中的所有名称我怎么能做任何一个请帮助我。

提前感谢你。

2 个答案:

答案 0 :(得分:1)

尝试在标题中添加BOOL,然后执行以下操作:

-(IBAction)buttonPressed:(id)sender{
    myBoolean = YES;
    [tableView reloadData];
    myBoolean = NO;
}

然后,在cellForRowAtIndexPath方法中,只需添加:

if(myBoolean){
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}

答案 1 :(得分:0)

- (IBAction)selectAll:(id)sender
{
    if(myBoolean)
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];
            }
        }

        myBoolean = NO;
    }
    else
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryCheckmark];
            }
        }

        myBoolean = YES;
    }
}