我有一個佔用點的人列表。我希望用戶能夠將這些人重新安排到不同的地方,但是有些地方是禁止的。我認爲使用UITableView的重新排列功能很容易實現。但是,我無法弄清楚如何讓不可用的斑點保持靜止。當在UITableView中移動單元格時,如何讓單元格保持靜止?
例如,我想將Activia Boulanger移到第5點。灰色細胞應該是不可移動的細胞。
開始觀看:
的UITableView的自動做些什麼:
我想要的UITableView做什麼:
設置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
你是如何表示你的人員數組中的空白?在支持此表的數據源數組中。 – danh 2012-04-18 02:14:42
它們是空條目:[NSNull null]。當我遇到一個空條目時,我創建了一個「不可移動」的單元格類型。 – LDY 2012-04-18 02:16:06
移動之後,你可以重新排列你的陣列,將零點移回到位置3,5,7等嗎? – danh 2012-04-18 02:18:32