2012-04-18 176 views
4

我有一個佔用點的人列表。我希望用戶能夠將這些人重新安排到不同的地方,但是有些地方是禁止的。我認爲使用UITableView的重新排列功能很容易實現。但是,我無法弄清楚如何讓不可用的斑點保持靜止。當在UITableView中移動單元格時,如何讓單元格保持靜止?

例如,我想將Activia Boulanger移到第5點。灰色細胞應該是不可移動的細胞。

開始觀看:

Beginning

的UITableView的自動做些什麼:

What UITableView does

我想要的UITableView做什麼:

What I want

設置tableView:canMoveRowAtIndexPath:似乎只是阻止您移動細胞,但不會阻止細胞對其他細胞的移動作出反應。

任何幫助將不勝感激。 感謝


UPDATE: 下面是針對該問題的設置一些示例代碼。我沒有將我的努力納入解決方案,因爲他們都失敗了,會讓事情混亂起來。

#import "LDYViewController.h" 

static NSString * unmoveableCellId = @"NoMove"; 
static NSString * moveableCellId = @"OkMove"; 

@implementation LDYViewController 
@synthesize tableView; 
@synthesize peopleList; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    peopleList = [[NSMutableArray alloc] initWithObjects: 
        @"Belinda Boomer", @"Activia Boulanger", @"Arnold Carter", [NSNull null], @"Attila Creighton", [NSNull null], @"Bruce Cleary", [NSNull null], nil]; 
    [tableView setEditing:YES]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return peopleList.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 

    if([peopleList objectAtIndex:indexPath.row] == [NSNull null]) { 
     cell = [tableView dequeueReusableCellWithIdentifier:unmoveableCellId]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unmoveableCellId]; 
      cell.userInteractionEnabled = NO; 
      cell.contentView.backgroundColor = [UIColor grayColor]; 
     } 
    } else { 
     cell = [tableView dequeueReusableCellWithIdentifier:moveableCellId]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:moveableCellId]; 
      NSString * name = [peopleList objectAtIndex:indexPath.row]; 
      cell.textLabel.text = name; 
     }   
    } 

    return cell; 
} 

- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { 
} 

- (NSIndexPath *)tableView:(UITableView *)tableView 
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
     toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { 
    return proposedDestinationIndexPath; 
} 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([peopleList objectAtIndex:indexPath.row] == [NSNull null]) { 
     return NO; 
    } 

    return YES; 
}  
@end 
+0

你是如何表示你的人員數組中的空白?在支持此表的數據源數組中。 – danh 2012-04-18 02:14:42

+0

它們是空條目:[NSNull null]。當我遇到一個空條目時,我創建了一個「不可移動」的單元格類型。 – LDY 2012-04-18 02:16:06

+0

移動之後,你可以重新排列你的陣列,將零點移回到位置3,5,7等嗎? – danh 2012-04-18 02:18:32

回答

2

做你的模型在正常重排的邏輯,那麼當它這樣做,把空回在你要他們。發佈的代碼不包括模型,所以這裏是一個合理的模型來說明:

// always leave our array with a null at indexes 3,5,7 
// i'm sure you have better logic about where nulls go, but just to illustrate 
- (void)addNulls { 

    NSMutableArray *answer = [NSMutableArray array]; 
    for (id object in self.peopleList) { 
     if (![object isKindOfClass:[NSNull self]]) { 
      [answer addObject:object]; 
     } 
    } 

    assert(answer.count >= 4); 
    [answer insertObject:[NSNull null] atIndex:3]; 
    [answer insertObject:[NSNull null] atIndex:5]; 
    [answer insertObject:[NSNull null] atIndex:7]; 
    self.peopleList = answer; 
} 

// build a pretend model with nulls 
- (NSMutableArray *)peopleList { 

    if (!_peopleList) { 
     _peopleList = [NSMutableArray array]; 
     [_peopleList addObject:@"Moe"]; 
     [_peopleList addObject:@"Larry"]; 
     [_peopleList addObject:@"Curly"]; 
     [_peopleList addObject:[NSNull null]]; 
     [_peopleList addObject:@"Groucho"]; 
     [_peopleList addObject:[NSNull null]]; 
     [_peopleList addObject:@"Chico"]; 
     [_peopleList addObject:[NSNull null]]; 
     [_peopleList addObject:@"Harpo"]; 
    } 
    return _peopleList; 
} 



// normal rearrange logic, then fix your array up 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 

    // reorder the list with the typical logic, ignoring nulls 
    NSString *stringToMove = [self.peopleList objectAtIndex:fromIndexPath.row]; 
    [self.peopleList removeObjectAtIndex:fromIndexPath.row]; 
    [self.peopleList insertObject:stringToMove atIndex:toIndexPath.row]; 

    // now put nulls into the positions you want them 
    [self addNulls]; 
    [self.tableView reloadData]; 
} 
+0

出於某種原因,重載數據似乎將所有內容都放回原來的位置,而不是模型指定的位置。我已經仔細檢查了最終的模型,沒關係。你的行爲有不同嗎?感謝你的幫助。 – LDY 2012-04-19 11:07:40

+0

我在我自己的表格vc中檢查過這段代碼,它工作正常。該模型沒有改變是證明moveRowAtIndexPath不起作用或被調用。嘗試在那裏放置一個斷點,然後檢查模型。 – danh 2012-04-19 14:08:24

+0

我想清楚我的問題是什麼。這是非常愚蠢的:當我重繪單元格時,我在測試空單元格下嵌套了我的名稱分配,因爲行x中的單元格已經存在,所以它不會重寫名稱。謝謝你的耐心!現在,只要我能做到這一點,灰色的條不會在動畫中上滑。任何想法? :) – LDY 2012-04-20 12:38:58

0

您可以通過這樣做:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{ 

在這種方法中,你需要檢查從indexIndexPath和toIndexPath檢查,如果用戶移動到一個無效的位置。將tableview的數據源修改爲放回單元格或將其移動到正確的位置。

+0

我給了這個嘗試。如果我試圖將Activia移動到位置4,我將toIndexPath更改爲位置5.但是,這最終移動了單元格4和5,就好像它們是一個塊一樣,導致與圖像2的位置相同。 – LDY 2012-04-18 02:22:05

+0

實際上,我我想我可能誤解了你的回答。我會試試這個。 – LDY 2012-04-18 02:32:22

+0

我試着修改數據源來匹配我想要的視圖。即使重新加載表後,這似乎沒有幫助。我查了一下'[UITableView reloadData]',它說「不應該在插入或刪除行的方法中調用它」。也許這就是爲什麼重新加載不起作用?我剛剛添加了一些代碼來解決這個問題。謝謝你的幫助。 – LDY 2012-04-19 01:56:58

8

試試這個:

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPath 
     toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 

您可以檢查proposedDestinationIndexPath,如果你不想動行,你可以返回sourceIndexPath則行將被移回,否則返回proposedDestinationIndexPath。

+0

在上面給出的例子中,索引5是單元格的允許位置,但是,我的灰色單元格仍然移動。這個問題不是我可以將細胞移動到我的灰色細胞的位置,而是灰色細胞響應其他細胞的移動而移動。我試過類似這樣的東西:如果我嘗試將一個單元格移動到灰色單元格的位置,我會增加該行,以便強制將單元格移動一個較低的單元格。這可以防止我將單元格移動到索引4(灰色單元格所在的位置),但是灰色單元格仍然與其他所有單元格一起向上移動,並且我仍然以圖像2結尾。 – LDY 2012-04-18 14:17:13

+0

然後,我想在此主題中的所有建議將不會幫助你,你應該嘗試這樣的: - 分成3組(早上,下午,晚上),每組將會是你桌子上的一部分。 - 每個組都有固定數量的斑點,然後當您將某個項目從一個區域移動到另一個區域時,首先,選中要移動的項目以替換已移動的項目。 :) – 2012-04-19 04:03:45

2

我面臨類似的問題,並解決它。

創建2個部分,並把所有灰色排在第2

-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{ 

if (proposedDestinationIndexPath.section == 1) 
    return sourceIndexPath; 
else 
    return proposedDestinationIndexPath; 

}

+1

難道這不會把我所有的灰色單元組合在一起嗎? – LDY 2012-04-18 14:16:06

+0

爲我完美工作!謝謝 – DocAsh59 2017-02-22 20:22:42

相關問題