2014-07-10 13 views
1

我想在UITableView的第一行中添加一些特殊效果,但是当我向下滚动表格视图时,它也会与其他单元格一起显示(因为单元格的可重用性?)。那么有没有什么办法可以防止任何特定的细胞被重复使用?我尝试将nil作为reusableIndentifier传递给第一个细胞,但它给我带来了错误(插入失败)。有没有可能不重用UITableView中的特定单元格?

我打电话从viewDidApper法这种方法对于一些动画的第一行向

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

} 

但是当我滚动表视图我得到几个其他细胞中,这种动画。 任何帮助或建议,将不胜感激。

+0

如果对这些单元使用不同的'reusableIndentifier',它应该可以工作。 reusableIndentifier是为了命名单元格的类型..所以你总是得到正确的。 – Bastian

+0

你在哪里调用openSubmenuForFirstRow? – mxb

+0

@mxb正如我在'viewDidAppear'方法中提到的那样 – Bharat

回答

2

您可以使用两个单元CellIdentifires。一个是第一排,第二个是其他人。检查indexPath.row。如果它是0,请使用cellidentifier1,否则使用cellidentifier2。

用它来做第一行:

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"]; 
     } 
} 
+0

所以这种方法,我需要设计故事板中的两个单元格? – Bharat

+0

initWithFrame在iOS 3.0中首次被弃用,现在可以使用吗? – Bharat

+0

您需要使用initWithStyle而不是initWithFrame,还需要使用不同的cellIdentifiers来设计两个单元格。 – Ritu

0

您可以显示只有在显示第一个单元格的菜单。从viewDidAppear删除呼叫并添加这个方法:

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

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

你还需要改变你的方法如下:

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

} 
+0

我试过这个,但它不显示菜单时出现表格的时间,菜单显示后,我滚动表,但仍然是同样的问题,许多行显示菜单。 – Bharat

0

使用此代码寄存器单元:

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

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

objectAtIndex是您的自定义单元标记。

相关问题