简体   繁体   中英

iOS TableView Loading Same Cell

I am using the following code to have a tab view. When you click one tab it expands downwards, this is currently working okay but if I open the first tab with the indexPath of 0 which should load a separate custom opened view it will load but then when I go to the other indexPath tabs it displays the same view but if I reload the app and go to the other tabs first they load the correct view. It's nearly like the app is making a cache?

Is there any way to solve this??

Thanks

- (void)viewDidLoad {
    row = 1000;
     NSLog(@"row is %d", row);
    [super viewDidLoad];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    infoStory = [userDefaults objectForKey:@"storyname"];
    // Do any additional setup after loading the view.
    numbers = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", nil];

    // Initialize thumbnails
    text = [NSArray arrayWithObjects: @"Arriving by Bus or Train", @"Arriving by Car (Autobahn 5)", @"Arriving by Car (Autobahn 6)", @"Wifi", @"Registration", @"Refreshments", @"Evening Event", @"Contact Information", nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(row == indexPath.row){
        if(indexPath.row == 0){
            static NSString *simpleTableIdentifier2 = @"TableViewCellOPENEDbustaxi2";
            TableViewCell1 *cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
            if (cell == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCellOPENEDbustaxi2" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }

            cell.text.text = [text objectAtIndex:indexPath.row];
            return cell;
        }
        else{
            static NSString *simpleTableIdentifier = @"TableViewCellOPENED";
            TableViewCell1 *cell2 = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
            if (cell2 == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCellOPENED" owner:self options:nil];
                cell2 = [nib objectAtIndex:0];
            }

            cell2.text.text = [text objectAtIndex:indexPath.row];
            return cell2;
        }

    }
    else{
    static NSString *simpleTableIdentifier = @"TableViewCellINFO";

    TableViewCell1 *cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCellINFO" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
        cell.text.text = [text objectAtIndex:indexPath.row];
    return cell;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return [numbers count];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    if(row == indexPath.row){
        row = 1000;
        NSLog(@"row is %d", row);
        [self.tableViewObject reloadData];
    }
    else{
        row = indexPath.row;
        NSLog(@"row is %d", row);
        [self.tableViewObject reloadData];
    }


}

@end

I think the problem might be that you did not specify the correct reuse identifier in the attributes inspector of the cell in your XIB file(s). You should check if you use the wrong one somewhere, so dequeueing will return an unexpected cell.

You could also set the appropriate reuseIdentifier manually, with a little setValue:forKey: trick:

static NSString *simpleTableIdentifier2 = @"TableViewCellOPENEDbustaxi2";
TableViewCell1 *cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCellOPENEDbustaxi2" owner:self options:nil];
    cell = [nib objectAtIndex:0];
     [cell setValue:simpleTableIdentifier2 forKey:@"reuseIdentifier"];
}

and so on for the other cells and identifiers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM