1

I'm very new to iPhone programming, so my problem might be caused by total lack of knowledge of very basic principles.

I'm developing an iPhone app that has two Views. The first view has two buttons, when one of the buttons is pressed, a modalview popsup with a tableview. This table view is populated differently depending of what button is pressed. If button button1 is pressed the tableview is populated with the button1Data. The user can select cells, and if this is done the cells accessorytype is set to UITableViewCellAccessoryCheckmark. I then save the name of the checked cells into tableViewListChecked, so it can later be decided what cell are supposed to be checked as the data changes.

The problem is as following: after the modalview is dismissed, and I select button2 the cells that was selected in button1Data is still selected in button2Data. It is clear to me that the function [theTableView reloadData] is working since the data does change, however the accesorytupes is still the same until i scroll the cells off the screens.

P.S. If I do in fact scroll cells of the screen there accessorytype is set correctly.

- (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.accessoryType = UITableViewCellAccessoryNone;
    }

    else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }

    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    cell.textLabel.text = [tableViewList objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (![tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [tableViewListChecked addObject:[NSString stringWithString:cell.textLabel.text]];       
    }

    else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [tableViewListChecked removeObject:[NSString stringWithString:cell.textLabel.text]];
    }   
}

Thanks for you help!

AndHeiberg
  • 1,029
  • 1
  • 10
  • 29

3 Answers3

1

If the cell is not nil (if it has been dequeued from the table view) then you don't actually, as it is, change the accessory type. Drop the else, ie change

else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
}

to

if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
}
jbat100
  • 16,757
  • 4
  • 45
  • 70
  • Thanks for the answer, however the app crashes if I do this. Here's the console output http://pastie.org/2677067 you might be able to make sense of it, but I'm not that long into my "training". – AndHeiberg Oct 11 '11 at 14:11
  • Probably because you have [NSString stringWithString:cell.textLabel.text] where cell.textLabel.text is nil which raises an exception. – jbat100 Oct 11 '11 at 14:29
  • TAmazing, I moved the if statement down below the point at which I set the cell.text. It's working great at this point. Thank you, you just saved my day. – AndHeiberg Oct 11 '11 at 14:59
0

It sounds like you are reusing the same instance of the UITableView each time you present the modal view, in which case, what is happening to the tableViewListChecked array? You are swapping out data in the tableViewList; but are you employing some strategy to persist the list of checked items between taps on button1 and button2?

FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42
  • the tableViewListChecked array is allocated by me, so as far as I understand, it isn't deleted until the program quits. The array doesn't need to be saved after quitting, so I simply don't release it. That is my strategy, it might not be ideal, but so far it work great. Thanks for your answer. – AndHeiberg Oct 11 '11 at 14:15
0

If the guys here didn't helped you, you can just set: cell.accessoryType = UITableViewCellAccessoryNone every time the user clicks on button2

Dor Bashan
  • 136
  • 2
  • I like your thinking, however I don't know how to set every cells accesorrytype to none from the other veiw. Could you explain that to me? Thanks for you reply. – AndHeiberg Oct 11 '11 at 14:22
  • Uhm..haven't tested my idea but i think you can define a bool value and set it to NO at the viewDidLoad. then when the user clicks on button2 set this bool to YES and save it in the userDefaults (if you don't know how i'll show a code) then when the other view comes up check this bool from the memory and in your cellForRowAtIndexPath method check this bool and set your cells.. as i mentioned before – Dor Bashan Oct 11 '11 at 15:46
  • this might be a solution, however I won't test it since jBat already gave me a great answer that will let me keep all the code in the modal view controller :) But thanks again – AndHeiberg Oct 11 '11 at 20:51