2011-03-27 17 views
0

我有一個UITableView放在編輯模式。iPhone - 如何添加最後一行在UITableView添加項目,防止這條線的重新排列

self.tableView.editing = YES; 

在該表視圖,我有一個自定義的單元格中顯示的某些行,我想添加一個在端部將允許用戶添加的項(使用另一個視圖)。

所以我寫了一些臺詞:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return "number of lines" + 1; 
} 

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(indexPath.row+1 != [tableView numberOfRowsInSection:0]) { 
     return UITableViewCellEditingStyleDelete; 
    } 
    else { 
     return UITableViewCellEditingStyleInsert; 
    } 
} 

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

    if (indexPath.row + 1 == [tableView numberOfRowsInSection:0]) { 
     cell.textLabel.text = @"Add a line..."; 
    } 
    else { 
     do the stuff in the custom cell 
    } 
} 

做這樣一來,UITableView允許重新排列任何線路。我可以在「添加行」之後移動第一行,並將「添加行」移動到第一位置。

我該如何刪除「添加行」單元格上的排列按鈕,並防止其他行在這個下面?

或者是否有更好的方法來編碼?

回答

3

實施

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return ([indexPath row] != INDEX_OF_SPECIAL_ROW) 
} 

- (NSIndexPath *)tableView:(UITableView *)tableView 
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
     toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 
{ 
    if ([proposedDestinationIndexPath row] < NUMBER_OF_ROWS) 
    { 
     return proposedDestinationIndexPath; 
    } 

    NSIndexPath *otherPath = [NSIndexPath indexPathForRow:NUMBER_OF_ROWS-1 inSection:0]; 

    return otherPath; 
} 
3

的更容易的方法是設置一個tableFooterView。您可以將任何UIView放在那裏,所以您不僅可以添加UITableViewCell,而且還可以添加完全自定義的子類。這樣做,你會避免所有不必要的檢查。

相關問題