2011-01-28 13 views
1

我在tableview中顯示一個名稱爲數組的數組。如何檢查在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]; 
} 

現在我需要放置一個按鈕全選時曾經使用過一下就可以了,我需要檢查所有名稱表中的我怎麼能做到任何一個可以請幫我。

謝謝你提前。

回答

1

嘗試把一個BOOL在你的頭,那麼這樣做:

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

然後,cellForRowAtIndexPath方法中,只補充一點:

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

感謝ü奧羅拉奎拉,如果我通過點擊全選按鈕來使用這段代碼,所有的表格行都會被選中,但是當我在選中的行上選擇來取消選中它時,它們都不會被選中。 – MaheshBabu 2011-01-29 04:20:41

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; 
    } 
} 
相關問題