2014-07-10 12 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是您的自定義單元標記。

相關問題