《iOS总结》UITableViewCell的增加与删除-MickyChiang
来源:互联网 发布:qq的smtp服务器端口 编辑:程序博客网 时间:2024/06/06 02:05
UITableViewCell的增加与删除
我把代码都贴进来 看不懂的可以联系我= =
// 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
// 运行后的效果
图一:
图二/图三:
图四:
0 0
- 《iOS总结》UITableViewCell的增加与删除-MickyChiang
- 《iOS总结》设计模式-MickyChiang
- 《iOS总结》推送消息-MickyChiang
- 《iOS总结》UIKit框架-MickyChiang
- iOS遇到的那些事(持续总结并更新)-MickyChiang
- iOS UITableviewCell的删除
- ioS学习 UITableViewCell 增加、删除、移动
- 《iOS总结》传值方式-MickyChiang
- 《iOS总结》OC内存管理-MickyChiang
- 《iOS总结》数据持久化-MickyChiang
- UITableViewCell的操作(增加cell,删除cell,插入cell)
- iOS - UITableViewCell滑动删除
- iOS UITableViewCell滑动删除
- UITableViewCell的移动、插入与删除
- UITableView详解(UITableViewCell(四) 增加 删除 移动)
- iOS UITableViewCell左滑删除
- iOS开发-UI控件:UITableViewCell划动删除的实现
- iOS UITableViewCell侧滑删除遇到的坑
- lamp环境下安装Zabbix环境
- UILabel设置多种字体、颜色 (NSMutableAttributedString)
- java 图片读写
- oracle XE精简版不支持FETCH FIRST 5 ROWS ONLY语句
- source 1.3 中不支持泛型 解决办法
- 《iOS总结》UITableViewCell的增加与删除-MickyChiang
- linux中sd卡驱动(2)
- [.Net码农]RDLC报表打印时提示 客户端无法加载打印控件的问题
- Hadoop单机伪分布式配置
- android:强大的图片下载和缓存库Picasso
- 基于zookeeper的分布式Queue
- [.Net码农]RDLC打印控件失败
- 1075 - Incorrect table definition;there can be only one auto column and it must be defined as a key
- 使用提交按钮,提交数据