2016-03-24 37 views
-2

Tablo görünümünde bir satırı kaldırmak için birçok yol deneyin ancak şimdilik bunların hiçbiri işe yaramıyor. Kullanıcı sadece üzerinde uzun bir baskı uygulandığında tableView satırını kaldırmak istiyorum.Tablo görünümü satırı kaldırılamıyor

ChecklistViewController.h

#import <UIKit/UIKit.h> 

@interface CheckListViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> 

@end 

ChecklistViewController.m

@interface CheckListViewController() 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
@end 

@implementation CheckListViewController { 
NSMutableDictionary *_checkFlag; 
NSArray *_menuData; 
NSMutableArray *_edit; 
NSMutableArray *_inputCell; 
NSInteger rowToremove; 
NSIndexPath *theIndexPathe; 
int _maxInputCell; 
} 

- (IBAction)barAddTapped:(id)sender { 

if (_maxInputCell < 3){ 

_maxInputCell +=1; 
[_inputCell addObject:[NSNull null]]; 
[self.tableView reloadData]; 

CGPoint offset = CGPointMake(0, self.tableView.contentSize.height -  self.tableView.frame.size.height); 
[self.tableView setContentOffset:offset animated:YES]; 
} 
} 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
_maxInputCell = 1; 
_checkFlag = [[NSMutableDictionary alloc]init]; 
_inputCell = [[NSMutableArray alloc]initWithCapacity:_maxInputCell]; 

for(int i=0;i<_maxInputCell;i++) 
    [_inputCell addObject:[NSNull null]]; 

_menuData = @[ 
       @"Passeport",@"Carte membre",@"Carte d'embarquement",@"Visa",@"Autorisation",@"Meddicaments autorisés",@"Produits autorisés"]; 

_edit = [NSMutableArray arrayWithObjects:@"text1",nil]; 

self.tableView.backgroundColor = [UIColor colorWithWhite:255 alpha:.6f]; 
self.tableView.layer.cornerRadius=10.0f; 
self.tableView.clipsToBounds=YES; 

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
             initWithTarget:self action:@selector(handleLongPress:)]; 
lpgr.minimumPressDuration = 1.5; //seconds 
lpgr.delegate = self; 
[self.tableView addGestureRecognizer:lpgr]; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

NSLog(@"%d",[_menuData count] + _maxInputCell); 
return [_menuData count] + _maxInputCell; 
} 

- (IBAction)returnKeyPressed:(id)sender { 
[sender resignFirstResponder]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell; 
long textFieldIndex = indexPath.row - [_menuData count]; 

if(textFieldIndex>= 0 && textFieldIndex < _maxInputCell) { 
    //Input cell 
    cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"]; 
    UITextField *textField; 

    if(!_inputCell[textFieldIndex]) { 
     _inputCell[textFieldIndex] = (UITextField *)[cell viewWithTag:1]; 
    } 
    textField = _inputCell[textFieldIndex]; 
} else { 
    //Label cell 
    cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"]; 
    cell.textLabel.text = [_menuData objectAtIndex:indexPath.row]; 
} 

if(_checkFlag[indexPath]!=nil) 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
else cell.accessoryType = UITableViewCellAccessoryNone; 

return cell; 
} 

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
if(_checkFlag[indexPath]!=nil) { 
    [_checkFlag removeObjectForKey:indexPath]; 
}else _checkFlag[indexPath][email protected]"1"; 

[self.tableView reloadData]; 
return nil; 
} 

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
CGPoint p = [gestureRecognizer locationInView:self.tableView]; 

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; 
if (indexPath == nil) { 
    NSLog(@"long press on table view but not on a row"); 
} else if (gestureRecognizer.state == UIGestureRecognizerStateBegan && indexPath.row > 6) { 
    NSLog(@"long press on table view at row %ld", (long)indexPath.row); 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Voulez-vous supprimer cette ligne ?" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Annuler",nil]; 
    alert.tag = 70; 
    [alert show]; 
    rowToremove = indexPath.row - 7; 
    theIndexPathe = indexPath; 
} 
else { 
    NSLog(@"gestureRecognizer.state = %ld", (long)gestureRecognizer.state); 
} 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
if (buttonIndex == 0 && alertView.tag == 70){ 
    NSLog(@"Yeah, %ld",(unsigned long)[_inputCell count]) ; 
    [self.tableView beginUpdates]; 
    [_inputCell removeObjectAtIndex:rowToremove]; 

    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:theIndexPathe] withRowAnimation:UITableViewRowAnimationFade]; 
    [self.tableView endUpdates]; 

} 
if (buttonIndex == 1 && alertView.tag == 70){ 
    NSLog(@"Annuler, %ld",(long)rowToremove); 

} 
} 

@end 

birisi bana yardımcı olabilir, önceden tarafından teşekkürler.

Tür da satır (yeni) doğru sayısını döndürmek için veri kaynağını güncellemek gerektiğini bir satır çıkarmadan önce

+0

Tek başına biçimlendirme, kodunuzu okumak neredeyse imkansız hale getirir. – vikingosegundo

cevap

0

yerine tableview jest uygulanması Eğer herhangi bir hücreyi harekete almak istiyorsanız hücrede jest eklemek gerekir

Kodunuz bu

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell; 
    long textFieldIndex = indexPath.row - [_menuData count]; 

    if(textFieldIndex>= 0 && textFieldIndex < _maxInputCell) { 
     //Input cell 
     cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"]; 
     UITextField *textField; 

     if(!_inputCell[textFieldIndex]) { 
      _inputCell[textFieldIndex] = (UITextField *)[cell viewWithTag:1]; 
     } 
     textField = _inputCell[textFieldIndex]; 
    } else { 
     //Label cell 
     cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"]; 
     cell.textLabel.text = [_menuData objectAtIndex:indexPath.row]; 
    } 

    if(_checkFlag[indexPath]!=nil) 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    else cell.accessoryType = UITableViewCellAccessoryNone; 

    //**** 
    //Added gesture UILongPressGestureRecognizer here instead of tableview 

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
              initWithTarget:self action:@selector(handleLongPress:)]; 
    lpgr.minimumPressDuration = 1.5; //seconds 
    lpgr.delegate = self; 
    [cell addGestureRecognizer:lpgr]; 

    return cell; 
} 

ve sizin gibi bir şey olacak eski kodunda yaptığı gibi 'handleLongPress' işleyebilir


Not: boylam ayarı hiçbir gereklilik varsa tableView silmek gr basın şuna benzer olacaktır Seçili satırı silmek için de varsayılan seçeneği vardır, slayt böyle olabilir silme düğmesi için sil seçeneğini

Default delete tableview

Kod almak için:

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
} 
+0

Sorunu buldum, sadece clickedButtonAtIndex yöntemimde '_maxInputCell - = 1;' öğesini eklemeyi unuttum. Acemi hatası. Ancak, tablo görünümünün seçili satırı bir slayt hareketi ile silme varsayılan seçeneği olduğunu bilmiyordum, bunu kullanacağım. Teşekkürler @Pyro –

0

ilişkin fakat sonra güncelleştirmeleri başlar. Bunu denedin mi? Eğer AllowEditing için UITableViewDelegate yöntemini uygulamış ise

+0

Evet, bunu denedim ama işe yaramıyor. –

-1

Ben O kaydırarak sil seçeneğini getirecek, ama tabii ki bunu çağırabilirsiniz bunu etkin değilse,

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.Delete { 
     numbers.removeAtIndex(indexPath.row)  
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
    } 
    } 

uygulamak lütfen emin değilim Uzun basın hareketi için delege yöntemi.

+0

yanlış dil. – vikingosegundo

0

Tamam Sadece hatamı buldum. ClickedButtonAtIndex yöntemimde _maxInputCell -= 1; eklemem gerekiyor. Acemi hatası. Bana yardım etmek için zaman ayıran herkese teşekkürler. Saygılar.

İlgili konular