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
Tek başına biçimlendirme, kodunuzu okumak neredeyse imkansız hale getirir. – vikingosegundo