Bharat
Bharat

Reputation: 3007

Is it possible not to reuse a specific cell in UITableView?

I want to put some special effect in first row of UITableView but when i scroll down the table view it comes with other cells as well(because cell reusability?). So is there any way that i prevent any specific cell from reuse?I tried with passing nil as reusableIndentifier for first cell but it gives me error(insertion failure).

I'm calling this method from viewDidApper method for some animation on first row-

-(void)openSubmenuForFirstRow{

    stIndex = 0;
    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    StoreCell* sCell = (StoreCell *)cell;

    UIView* mainView = [sCell viewWithTag:101];
    UIView* subView = [sCell viewWithTag:102];

    [UIView animateWithDuration:0.3 animations:^{

        CGRect rect = subView.frame;
        rect.origin.x = 0;
        subView.frame = rect;

        CGRect mainViewRect = mainView.frame;
        mainViewRect.origin.x = 117;
        mainView.frame = mainViewRect;
    }];

}

but i get this animation on several other cells when i scroll the table view. Any help or suggestion would be appreciated.

Upvotes: 1

Views: 796

Answers (3)

iBhavin
iBhavin

Reputation: 1261

Use this code for register cell :

 NSString *CellIdentifier = @"CustomCell";
    CustomCell  *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0];
    }

and where objectAtIndex is your custom cell tag.

Upvotes: 0

Ritu
Ritu

Reputation: 671

You can use two cell CellIdentifires. One is for first row and second for others. Check for indexPath.row. If it is 0 use the cellidentifier1 otherwise cellidentifier2.

Use it for first row:

 UITableViewCell* cell;
 if(indexPath.row == 0){
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1"];
 }
 else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell2"];
 }
 if (cell == nil) {
      if(indexPath.row == 0){
             cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell1"];
      }
      else{
           cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell2"];
      }
 } 

Upvotes: 2

mxb
mxb

Reputation: 3330

You can show the menu only when the first cell is displayed. Remove the call from viewDidAppear and add this method:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        [self openSubmenuForCell: cell];
    }
}

You'll also need to change your method as follow:

- (void)openSubmenuForCell: (UITableViewCell*)cell {

    stIndex = 0;
    StoreCell* sCell = (StoreCell *)cell;

    UIView* mainView = [sCell viewWithTag:101];
    UIView* subView = [sCell viewWithTag:102];

    [UIView animateWithDuration:0.3 animations:^{

        CGRect rect = subView.frame;
        rect.origin.x = 0;
        subView.frame = rect;

        CGRect mainViewRect = mainView.frame;
        mainViewRect.origin.x = 117;
        mainView.frame = mainViewRect;
    }];

}

Upvotes: 0

Related Questions