2013-10-24 3 views
0

Я использую пользовательские cell.I хотите переместить эту ячейку (изменить положение ячеек)Как перемещать пользовательскую ячейку uitableview?

я использовал эти делегации

UITableViewDataSource, UITableViewDelegate

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

static NSString *Cellidentifier = @"Celledit"; 
CellEdit *editCell =[tableView dequeueReusableCellWithIdentifier:Cellidentifier]; 

if (!editCell) { 
    editCell = [[CellEdit alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier]; 
} 

MItem *mItem = [mItems objectAtIndex:[indexPath row]]; 


--------------------------------- 
// set data 3 labales in the cell 
--------------------------------- 

editCell.editdelegate = self; 
return editCell; 

editcell мой заказ клетка

[_tableViewEdit setEditing:YES animated:YES]; эта линия положить в - (void)viewDidLoad

я хочу, чтобы изменить его всегда

// table view change cells 
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return UITableViewCellEditingStyleNone; 
} 

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

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

    NSInteger sourceRow = sourceIndexPath.row; 
    NSInteger destRow = destinationIndexPath.row; 
    id object = [minutesItems objectAtIndex:sourceRow]; 

    [minutesItems removeObjectAtIndex:sourceRow]; 
    [minutesItems insertObject:object atIndex:destRow]; 

} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 
    for (UIControl *control in cell.subviews) 
    { 
     if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellReorderControl")] && [control.subviews count] > 0) 
     { 
      for (UIControl *someObj in control.subviews) 
      { 
       if ([someObj isMemberOfClass:[UIImageView class]]) 
       { 
        UIImage *img = [UIImage imageNamed:@"logocoffee.png"]; 
        ((UIImageView*)someObj).frame = CGRectMake(0.0, 0.0, 43.0, 43.0); 
        ((UIImageView*)someObj).image = img; 
       } 
      } 
     } 
    } 
} 


} 

для перемещения тренажера ячейки не отображается значок с правой стороны управления

почему это? Это означает, что вы не можете переместить костюмы?

ответ

3

Пользовательские ячейки могут быть перемещены также.

Из ваших образцов кода мы не видим, где вы настраиваете режим просмотра таблицы в режиме редактирования.

Возможно, проблема заключается в том, что вы никогда не устанавливаете режим редактирования.

Если это так, попробуйте добавить кнопку Edit в строке заголовка, например, которые должны быть связаны с такими методами, как:

- (IBAction) EditTable:(id)sender{ 
if(self.editing) 
{ 
    [super setEditing:NO animated:NO]; 
    [yourTableView setEditing:NO animated:NO]; 
    [yourTableView reloadData]; 
    [self.navigationItem.leftBarButtonItem setTitle:@"Edit"]; 
} 
else 
{ 
    [super setEditing:YES animated:YES]; 
    [yourTableView setEditing:YES animated:YES]; 
    [yourTableView reloadData]; 
    [self.navigationItem.leftBarButtonItem setTitle:@"Done"]; 
} 
} 

Я думаю, что это один должен решить проблему.

Edit: Для того, чтобы возобновить в вашем случае, как минимум, вам нужно всего лишь установить вид таблицы в режиме редактирования в viewDidLoad, и обновлять его при необходимости:

[yourTableView setEditing:YES animated:YES]; 
[yourTableView reloadData]; 

и реализовать оба обратных вызовов :

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

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
    //Even if the method is empty you should be seeing both rearrangement icon and animation. 
} 

Я пробовал с пользовательскими ячейками, и он отлично работает.

+0

я поставил его в - (пустоте) viewDidLoad [_tableViewEdit setEditing: YES animated: YES]; эта строка линии Я хочу, чтобы он всегда редактировался. – Suravi

+0

Вы также называете метод супер, как в примере: '[super setEditing: YES animated: YES]'? – Lisarien

+0

Да, я добавляю его в viewDidLoad. как насчет метода ужина пользовательского UITableViewCell? – Suravi

3

Попробуйте этот код,

Вы должны использовать ниже два метода,

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

Используйте этот метод для перемещения строки

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
    [label1array exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row]; 
    [label2array exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row]; 
    [label3array exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row]; 
} 
+0

уже добавил выше методы – Suravi

Смежные вопросы