1.实现单元格的删除,实现效果如下
- (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @"编辑"; self.navigation.rightBarButtonItem = self.editButtonItem; [self initTableViewData]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)initTableViewData{ NSBundle *bundle = [NSBundle mainBundle]; NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"]; dataArr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataArr count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"tableCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSUInteger row = [indexPath row]; NSDictionary *rowDict = [dataArr objectAtIndex:row]; cell.textLabel.text = [rowDict objectForKey:@"itemName"]; NSLog(@"cell.label.text = %@",[rowDict objectForKey:@"itemName"]); NSString *imagePath = [rowDict objectForKey:@"itemImagePath"]; cell.imageView.image = [UIImage imageNamed:imagePath]; NSLog(@"cell.image.image = %@",imagePath); cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } //选中Cell响应事件 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失 NSUInteger row = [indexPath row]; NSDictionary *rowDict = [dataArr objectAtIndex:row]; NSString *userName = [rowDict objectForKey:@"itemName"]; NSLog(@"userName=%@",userName); } //返回编辑状态的style - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { //UITableViewCellEditingStyleInsert // return UITableViewCellEditingStyleNone; return UITableViewCellEditingStyleDelete; } //完成编辑的触发事件 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [dataArr removeObjectAtIndex: indexPath.row]; // [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] // withRowAnimation:UITableViewRowAnimationFade]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView reloadData]; } } //UIViewController生命周期方法,用于响应视图编辑状态变化 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:YES]; if (self.editing) { self.editButtonItem.title = @"完成"; } else { self.editButtonItem.title = @"编辑"; } } @end
2.移动单元格
//完成移动的触发事件,不添加该方法不实现移动功能 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSDictionary *item = [dataArr objectAtIndex:sourceIndexPath.row]; [dataArr removeObjectAtIndex:sourceIndexPath.row]; [dataArr insertObject:item atIndex:destinationIndexPath.row]; }
3.添加单元格。下面是自定义触发事件,即单击左下角的add按钮
- (IBAction)addistItem:(UIBarButtonItem *)sender { AppUtils *appUtils = [AppUtils alloc]; //需要先初始化一个UIAlertView UIAlertView *alert = [UIAlertView alloc]; [appUtils showInputDialogWithTitle:@"add" message:@"please input new user name:" toAlertView:alert confirmAction:(^{ //得到输入框 UITextField *textField=[alert textFieldAtIndex:0]; // 不要写成NSMutableDictionary *newItem = [NSDictionary dictionary]; NSMutableDictionary *newItem = [NSMutableDictionary dictionary]; [newItem setObject:textField.text forKey:@"itemName"]; [newItem setObject:@"1.jpeg" forKey:@"itemImagePath"]; [dataArr addObject:newItem]; [self.tableView reloadData]; })]; }
4.附上·AppUtils类
#import "AppUtils.h" #include "RIButtonItem.h" #include "UIAlertView+Blocks.h" @implementation AppUtils //弹出警告框,并实现警告框按钮的触发事件 - (void)showInputDialogWithTitle:(NSString *)title message:(NSString *)message toAlertView:(UIAlertView*) alert confirmAction:(void(^)(void))action{ RIButtonItem* cancelItem = [RIButtonItem item]; cancelItem.label = @"No"; cancelItem.action = ^ { //为NO时的处理 UITextField *tf=[alert textFieldAtIndex:0]; NSLog(@"UITextField=%@",tf.text); }; RIButtonItem* confirmItem = [RIButtonItem item]; confirmItem.label = @"Yes"; // confirmItem.action = action; alert = [alert initWithTitle:title message:message cancelButtonItem:cancelItem otherButtonItems:confirmItem, nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; confirmItem.action = action; [alert show]; } @end
相关推荐
StyledTableViewCell-for-iOS, 自定义表格视图单元格边框和突出显示 用于iOS的 StyledTableViewCell 自定义表格视图单元格边框和突出显示颜色。仅支持 UITableViewStylePlain特性表格视图单元格的自定义突出显示颜色
NULL 博文链接:https://ikrboy.iteye.com/blog/2004032
ios 表视图使用的例子,初步介绍了一些相关的东西
iOS展示视图移动的简单动画效果,点击目标视图自动伸缩
iOS UICollectionView实现单元格拖拽移动排序
NULL 博文链接:https://alan-hjkl.iteye.com/blog/1677016
iOS 弹出视图集成
新手一枚,做得不是很好,请多指教
简易的iOS开发切换视图示例, 适合新手
iPhone提供了4种基本的表格视图单元格
很简单的一个小方法,实现点击组头收起单元格
表视图是iOS开发中最重要的视图,它以列表的形式展示数据。表视图又一下部分组成: 表头视图:表视图最上边的视图 表脚视图:表视图最下边的视图 单元格(cell):表视图中每一行的视图 节(section):由多个单元格...
IOS 自定义单元格表视图. 注意:主要了解CustomerCell的使用。
可以实现动态添加需要的视图,例如button或者是imageview,对有需要上传多张图片的应用很有用。
此实例主要实现UITableViewDataSource协议中必须要实现的两个方法
不刷新表改变x区y行的某个数据 使表滑动到指定的行
ios 多视图切换 演示了ios中如何切换视图,切换的同时如何设置动画,和有关内存告急时的释放处理方式
作者IvanVorobei,源码ParallaxTableView,这是一个不错的视图视图与表视图单元格案例。
ios中自定义单元格,并从数据库中读数据显示的例子
较为典型的ios自定义视图 自定义视图多用来呈现出现频率较高的视图