开发者

Cell accessorytype doesn't reload on [tableview reloadData]

开发者 https://www.devze.com 2023-04-12 14:44 出处:网络
I\'m very new to iPhone programming, so my problem might be caused by total lack of knowledge of very basic principles.

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] aut开发者_StackOverfloworelease];
        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!


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


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号