UITableView 中徹底禁止移動某行

<p>
在UITableView中,我們可以使用- (BOOL) tableView: (UITableView *) tableView canMoveRowAtIndexPath: (NSIndexPath *) indexPath方法來禁止移動某一行。下面的例子是禁止移動最后一行。但是,雖然不能移動最后一行,卻可以將其他行移動至最后一行下方。
</p>
<code>

  • (BOOL)tableView:(UITableView *)tableView
    canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSArray *items = [[BNRItemStore sharedStore] allItems];

    if (indexPath.row + 1 == [items count]) {
    return NO;
    }
    return YES;
    }
    </code>

<p>我們可以使用- (NSIndexPath *) tableView: (UITableView *) tableView
targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath *) source
toProposedIndexPath: (NSIndexPath *) destination 方法來徹底禁止移動最后一行,使最后一行始終位于試圖的底部。例子如下:</p>

<code>
//prevent rows from being dragged to the last position:
-(NSIndexPath *) tableView: (UITableView *) tableView
targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath *) source
toProposedIndexPath: (NSIndexPath *) destination
{
NSArray *items = [[BNRItemStore sharedStore] allItems];
if (destination.row < [items count] - 1) {
return destination;
}
NSIndexPath *indexPath = nil;
// If your table can have <= 2 items, you might want to robusticize the index math.
if (destination.row == 0) {
indexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
} else {
indexPath = [NSIndexPath indexPathForRow: items.count - 2
inSection: 0];
}
return indexPath;
}
</code>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 版權聲明:未經本人允許,禁止轉載. 1. TableView初始化 1.UITableView有兩種風格:UITa...
    蕭雪痕閱讀 2,911評論 2 10
  • 一.進行重載數據的API 注意點:在操作這些方法的時候,記得要修改和保存數據來源,根據測試,該方法調用之后,會調用...
    討厭下雨的魚閱讀 2,119評論 0 1
  • 概述在iOS開發中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,085評論 3 38
  • 自定義單元格 表格無論有多少中自定義單元格樣式 每一種自定義單元格都有復用的能力所以每一個單元格都要帶有一個靜態局...
    DVWang閱讀 277評論 0 0
  • 靜靜放置在那的芭蕾舞鞋,在柔光中仿佛等待著穿他的人出現,跟著它一起去譜寫屬于他們的故事
    亞洲初戀閱讀 193評論 0 0