《iOS总结》UITableViewCell的增加与删除-MickyChiang
2015-10-22 18:09
507 查看
UITableViewCell的增加与删除
我把代码都贴进来 看不懂的可以联系我= =
// MyCell.h
// MyList.h
// ViewController.h
// 运行后的效果
图一:
图二/图三:
图四:
我把代码都贴进来 看不懂的可以联系我= =
// MyCell.h
#import <UIKit/UIKit.h> @interface MyCell : UITableViewCell @property (strong, nonatomic) IBOutlet UITextField *titleTextField; @property (strong, nonatomic) IBOutlet UITextView *contentTextView; @property (strong, nonatomic) IBOutlet UIButton *removeButton; @end
// MyList.h
#import <Foundation/Foundation.h> @interface MyList : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *content; @end
// ViewController.h
#import "ViewController.h" #import "MyCell.h" #import "MyList.h" @interface ViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UITextViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *objectArr; @property (nonatomic, strong) MyCell *myCell; @end @implementation ViewController - (void)dealloc { _tableView.delegate=nil; _tableView.dataSource=nil; } - (void)viewDidLoad { [super viewDidLoad]; [self change]; } - (void)change { if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) { self.navigationController.navigationBar.translucent = NO; } self.title=@"tableview的增删改查"; self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"保存" style:(UIBarButtonItemStylePlain) target:self action:@selector(saveCell:)]; self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStylePlain) target:self action:@selector(insertCell:)]; MyList *myList = [[MyList alloc] init]; myList.title=@"1"; myList.content=@"1"; [self.objectArr addObject:myList]; self.tableView.backgroundColor=[UIColor whiteColor]; } - (void)removeCell:(UIBarButtonItem *)item { if (self.objectArr.count==1) { if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=8.0)) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"不能全部删除" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:otherAction]; [self presentViewController:alert animated:YES completion:nil]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能全部删除" delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了", nil]; [alert show]; } } else { NSLog(@"删除一个cell"); [self.objectArr removeObjectAtIndex:self.objectArr.count-1]; NSLog(@"self.objectArr.count:%@", self.objectArr); NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; [self.tableView deleteRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationFade]; [self.tableView reloadData]; } } - (void)saveCell:(UIBarButtonItem *)item { NSLog(@"保存cell中的所有内容"); for (int i=0; i<self.objectArr.count; i++) { if ([[[self.objectArr objectAtIndex:i] title] isEqualToString:@""] && [[[self.objectArr objectAtIndex:i] content] isEqualToString:@""]) { [self.objectArr removeObjectAtIndex:i]; } } NSLog(@"保存后的self.objectArr:%@", self.objectArr); } - (void)insertCell:(UIBarButtonItem *)item { NSLog(@"添加一个cell"); MyList *myList = [[MyList alloc] init]; myList.title=[NSString stringWithFormat:@"%ld", self.objectArr.count+1]; myList.content=[NSString stringWithFormat:@"%ld", self.objectArr.count+1]; [self.objectArr insertObject:myList atIndex:self.objectArr.count]; NSLog(@"self.objectArr:%@", self.objectArr); NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; [self.tableView insertRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationTop]; [self.tableView reloadData]; } - (NSMutableArray *)objectArr { if (_objectArr==nil) { _objectArr=[NSMutableArray array]; } return _objectArr; } - (UITableView *)tableView { if (!_tableView) { _tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)]; _tableView.delegate=self; _tableView.dataSource=self; [self.view addSubview:_tableView]; } return _tableView; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.objectArr.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // return 100; MyCell *cell = (MyCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath]; return cell.frame.size.height; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"MyCell-%ld-%ld",indexPath.section,indexPath.row]; [_tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]; MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; MyList *myList=[self.objectArr objectAtIndex:indexPath.row]; cell.titleTextField.text=myList.title; cell.removeButton.tag=(indexPath.section+1)*1000+indexPath.row; [cell.removeButton addTarget:self action:@selector(removeClicked:) forControlEvents:(UIControlEventTouchUpInside)]; cell.titleTextField.delegate=self; cell.titleTextField.tag=(indexPath.section+1)*1000+indexPath.row; cell.tag=(indexPath.section+1)*1000+indexPath.row; cell.contentTextView.delegate=self; cell.contentTextView.text=myList.content; cell.contentTextView.tag=(indexPath.section+1)*1000+indexPath.row; return cell; } - (BOOL)textViewShouldEndEditing:(UITextView *)textView { NSLog(@"textViewShouldEndEditing"); NSLog(@"cell.contentTextView.tag:%ld", textView.tag); [textView resignFirstResponder]; return YES; } - (void)textViewDidEndEditing:(UITextView *)textView { NSLog(@"textViewDidEndEditing"); NSLog(@"cell.contentTextView.tag:%ld", textView.tag); MyList *myList=[self.objectArr objectAtIndex:textView.tag-1000]; if (![textView.text isEqualToString:myList.content]) { myList.content=textView.text; } else { NSLog(@"content的取值不变"); } } - (void)textFieldDidBeginEditing:(UITextField *)textField { NSLog(@"textFieldDidBeginEditing"); NSLog(@"cell.titleTextField.tag:%ld", textField.tag); } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { NSLog(@"textFieldShouldEndEditing"); NSLog(@"cell.titleTextField.tag:%ld", textField.tag); return YES; } - (void)textFieldDidEndEditing:(UITextField *)textField { NSLog(@"textFieldDidEndEditing"); NSLog(@"cell.titleTextField.tag:%ld", textField.tag); MyList *myList=[self.objectArr objectAtIndex:textField.tag-1000]; if (![textField.text isEqualToString:myList.title]) { myList.title=textField.text; } else { NSLog(@"title的取值不变"); } } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } #pragma mark - 删除cell - (void)removeClicked:(UIButton *)button { NSLog(@"buttonTag:%ld", button.tag); if (self.objectArr.count==1) { if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=8.0)) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"不能全部删除" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:otherAction]; [self presentViewController:alert animated:YES completion:nil]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能全部删除" delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了", nil]; [alert show]; } } else { NSLog(@"删除一个cell"); [self.objectArr removeObjectAtIndex:button.tag-1000]; NSLog(@"self.objectArr.count:%@", self.objectArr); NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; [self.tableView deleteRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationFade]; [self.tableView reloadData]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
// 运行后的效果
图一:
图二/图三:
图四:
相关文章推荐
- 基于zookeeper的分布式Queue
- UILabel设置多种字体、颜色 (NSMutableAttributedString)
- UINavigationController
- iOS SDK基础知识:UITextView & UITextViewDelegate
- 用UGUI实现小地图MiniMap
- requestFeature() must be called before adding content错误处理
- hdu2767 Proving Equivalences Tarjan缩点
- Hibernate中query的常用方法
- UIPanGestureRecognizer上下左右滑动方向判断算法
- *杭电1005——Number Sequence(数学题)
- ios7环境下如何禁用UINavigationController中的轻扫回退的手势
- Week5-1Syntactic structure: Constituency vs Dependency
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
- 关于UITableView的简单使用(1)
- protocol isis issues
- Android Quick Return Bar导航条
- 让UILabel的文字顶部对齐
- IOS中使用UITableView的group时,如何让第一行数据挨着顶部
- NGUI屏幕自适应
- ios多线程之GCD dispatch queue