2012-04-16 65 views
0

所以,有一个问题:当我检查表格中的单元格时,另一个单元格也检查,但我只想在一次检查。有源:多次检查一次accesoryType

- (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.text = [[self.symptoms objectAtIndex:indexPath.row ] objectForKey:@"name"]; 


    return cell; 
} 

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


    NSLog(indexPath); 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    } else { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
+0

有一个错字:'tableView:di:'应该读'tableView:didSelectRowAtIndexPath:'。 – Costique 2012-04-16 14:27:08

+0

Costique没有帮助:( – Hurrit 2012-04-16 14:39:42

回答

0

尝试不重复使用该单元格。 因为当您重复使用单元格时,您也可以重用复选标记/附件状态。 那么为什么不去除小区排队。

你可以试试:

.h文件中:

@property (nonatomic, retain) NSMutableDictrionary *checkedState; 

.m文件: (顶部)

@synthesize checkedState = checkedState_; 

(休息)

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

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:nil] autorelease]; 

    cell.textLabel.text = [[self.symptoms objectAtIndex:indexPath.row ] objectForKey:@"name"]; 

    if(self.checkedState) { 
     NSNumber *checkmarkStateNumber = [self.checkedState objectForKey:[NSNumber numberWithInt:indexPath.row]]; 
     if(checkmarkStateNumber && [checkmarkStateNumber boolValue] == YES) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 

    return cell; 
} 

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


    NSLog(indexPath); 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if(!checkedState) { self.checkedState = [NSMutableDictionary dictionary]; } 


    BOOL checkmarkState = NO; 
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    } else { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     checkmarkState = YES; 
    } 

    [self.checkedState setObject:[NSNumber numberWithBool:checkmarkState] forKey:[NSNumber numberWithInt:indexPath.row]]; 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

更新1: 现在它还应该存储复选标记状态!

+0

它帮助多重检查,但增加了一个新问题:现在,当我检查单元格时,向下滚动并向上滚动,单元格变为未选中 – Hurrit 2012-04-16 14:47:52

+0

是的,您需要将检查状态存储在Dataholder中。你可以使用NSMutableArray。你知道吗? – 2012-04-16 14:50:29

+0

哦,男人,谢谢你,工作得很好! – Hurrit 2012-04-16 15:07:54

0

以下属性添加到您的表视图控制器:

@property(nonatomic, retain) NSIndexPath* selectedIndexPath; 

合成财产在表视图控制器的实现:

@synthesize selectedIndexPath; 

此行添加到dealloc

self.selectedIndexPath = nil; 

将此添加到tableView:cellForRowAtIndexPath:

cell.accessoryType == (indexPath.section == self.selectedIndexPath.section && indexPath.row == self.selectedIndexPath.row) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

一下添加到tableView:didSelectRowAtIndexPath:

if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    self.selectedIndexPath = nil; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    self.selectedIndexPath = indexPath; 
} 

的优点是:

  • 你仍然可以重用的小区;
  • 即使视图由于内存不足警告而被卸载,它仍会在重新加载表视图后恢复复选标记。