2015-08-22 126 views
1

我需要保持我的第一個單元格始終位於tableview頂部,當我移動其他cell.I花了很多時間和許多方法按鈕,我還沒有弄清楚如何解決這個問題。 這是我的代碼:自定義移動tableview單元格:如何始終保持第一個單元格在UITableView頂部時「moveRowAtIndexPath」

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //...do something to custom first cell design from xib file 

    //...do some thing to custom normal cells(cells at below of first cell) 

    [firstcell setEditing:NO animated:YES]; 
    firstcell.userInteractionEnabled=NO; 

    if (indexPath.row==0) 
    { 
     return firstcell; 
    } 
    else 
    { 
     return cell; 
    } 
} 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) // Don't move the first row 
     return NO; 
    return YES; 
} 
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { 
    // i just change datasource for tableview at here 
} 

而且還有我的tableview當我移動的細胞(正常細胞)。

我想保持第一個單元格(藍色單元格)始終處於頂部,而不是與其他單元格交互。

回答

3

你需要實現一個更加委託方法:

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { 
    if (proposedDestinationIndexPath.row == 0) { 
     // Don't allow a row to be moved to the first row position 
     return [NSIndexPath indexPathForRow:1 inSection:0]; 
    } else { 
     return proposedDestinationIndexPath; 
    } 
} 

此代碼假設你只需要在你的表視圖一個部分。

該方法的要點是告訴表視圖,如果被移動的行的建議目標不合適,則應使用返回的值。如此處所寫,任何將行移動到頂部的嘗試都會導致它移動到最上面一行的下方。

相關問題