4

i m trying for apply for check mark in table view, but it is not working, if i checked again at that cell again then check mark apply. but not apply at new selected cell. any one there who help me....

thanks aamir.

i m using following code

#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];
}
smcdrc
  • 1,671
  • 2
  • 21
  • 29
Amir
  • 2,028
  • 6
  • 19
  • 28

2 Answers2

9

Do you mean that the checkmark doesn't apply when you choose the first line at the first time? You can add an else sentence to the code, but it wastes some time when running the code.

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;
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Chilly Zhong
  • 16,763
  • 23
  • 77
  • 103
  • i add else part also but it can't apply. checkmark only appear if i came back from my webview and again check that (past) cell. – Amir Aug 05 '09 at 09:43
  • Try to run the source code of the book directly, you may find out what's wrong by yourself. – Chilly Zhong Aug 05 '09 at 14:14
  • Little Change i have to change to do for proper working lastIndexPath = [indexPath retain]; – user930195 Nov 22 '11 at 14:22
2

There is a funny bug here

 if (row != oldRow)

because if oldRow was nil (or in another words... "0" ;-) and the user press the row = "0" the "if" sentence will think this row was checked.

I fixed like this:

- (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];
}

By the way... dont forget to use "self"

RubenVot
  • 188
  • 10