2009-08-05 6 views
4

テーブルビューでチェックマークを適用しようとしていますが、そのセルでもう一度チェックしてからチェックマークを適用すると機能しません。新しい選択されたセルには適用されません。 誰かが私を助けてくれます....iphoneのテーブルビューにチェックマークを適用するには?

ありがとうございます。

私は、次のコード

#pragma mark - 
#pragma mark Table Data Source Methods 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section 
{ 
    return [self.chaptersList count]; 
} 

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



static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CheckMarkCellIdentifier] autorelease]; 
} 

NSUInteger row = [indexPath row]; 
NSUInteger oldRow = [lastIndexPath row]; 

cell.textLabel.text = [chaptersList objectAtIndex:row]; 
cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? 
UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

return cell; 
} 

#pragma mark - 
#pragma mark Table Delegate Methods 
- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath 
{ 
    int row = [indexPath row]; 
    int oldRow = [lastIndexPath row]; 
    if (row != oldRow) 
    { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 

     lastIndexPath = indexPath; 
    } 

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

を使用することを忘れていけません。 – Aamir

+0

コードが実行されているときに最初の行を最初から選択することはできません。他の行を選択しても問題ありません。 –

+0

lastIndexPathを宣言して定義する場所???? –

答えて

8

あなたはあなたが初めてで、最初の行を選択するとチェックマークが適用されないことを意味するかを使用して、mは?コードに他の文を追加することはできますが、コードを実行すると時間が無駄になります。

if (newRow != oldRow) 
{ 
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
    newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
    oldCell.accessoryType = UITableViewCellAccessoryNone; 

    lastIndexPath = indexPath; 
} 
else 
{ 
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
    newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    lastIndexPath = indexPath; 
} 
+0

他の部分も追加しますが、適用できません。チェックマークは、私が私のwebviewから戻ってきたときにのみ表示され、その(過去の)セルを再度チェックします。 – Aamir

+0

本のソースコードを直接実行しようとすると、あなた自身が間違っていることが分かるかもしれません。 –

+0

ちょっとした変更私は正しい作業のために変更する必要がありますlastIndexPath = [indexPath retain]; – user930195

2

面白いバグがここ

if (row != oldRow) 

ありoldRowがnil(あるいは別の言葉で... "0" ;-)とユーザーを押したのであれば、行= "0" "if"文はこの行がチェックされたと考えます。

は、私はこのような固定:ところで

- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath 
{ 
    if (!self.lastIndexPath) { 
     self.lastIndexPath = indexPath; 
    } 

    if ([self.lastIndexPath row] != [indexPath row]) 
    { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 

     self.lastIndexPath = indexPath; 
    } 
    else { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

...私はそれに合わせ、私のコードは上記と同じであると思いますが、うまく機能していない「自己」

+0

[lastIndexPath isEqual:indexPath]を使用すると、 – malhal

関連する問題