2012-04-16 61 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; 
} 

的優點是:

  • 你仍然可以重用的小區;
  • 即使視圖由於內存不足警告而被卸載,它仍會在重新加載表視圖後恢復複選標記。