对TableView进行单元格的添加删除
2015-11-11 21:40
316 查看
对TableView进行单元格的添加删除
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.navigationItem.rightBarButtonItem = self.editButtonItem; self.navigationItem.title = @"单元格插入和删除"; self.textField.hidden = YES; self.textField.delegate = self; self.tableView.delegate = self; self.tableView.dataSource = self; self.listTeams = [[NSMutableArray alloc] initWithObjects:@"黑龙江",@"吉林",@"梁宁", nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:YES]; if (editing) { self.textField.hidden = NO; }else{ self.textField.hidden = YES; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.listTeams.count + 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; BOOL b_addCell = (indexPath.row == self.listTeams.count); UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } if (!b_addCell) { cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.textLabel.text = [self.listTeams objectAtIndex:indexPath.row]; } else { self.textField.frame = CGRectMake(10, 0, 300, 44); self.textField.text = @""; [cell.contentView addSubview:self.textField]; } return cell; } //编辑 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == [self.listTeams count]) { return UITableViewCellEditingStyleInsert; }else{ return UITableViewCellEditingStyleDelete; } } //提交编辑 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { [self.listTeams removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView reloadData]; }else if (editingStyle == UITableViewCellEditingStyleInsert){ [self.listTeams insertObject:self.textField.text atIndex:[self.listTeams count]]; [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView reloadData]; } } //高亮 - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == [self.listTeams count]) { return NO; }else{ return YES; } } //单元格高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 50; } //返回按钮 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } - (void)textFieldDidBeginEditing:(UITextField *)textField{ //返回textField的所在单元格 UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview]; [self.tableView setContentOffset:CGPointMake(0.0, cell.frame.origin.y) animated:YES]; }
相关文章推荐
- 技巧-如何在android项目中将布局文件存放在不同目录中
- DreamWeaver使用技巧(转)
- 排序算法-选择排序_简单选择排序
- 【iOS】集成支付宝钱包支付iOS SDK的方法与经验
- 6.c/c++程序员面试宝典-条件语句
- 图像直方图均衡化
- 【===博文导航===】
- json传递死循环问题
- Scala初步学习
- 社交关系中的共同好友数目计算(一度好友与二度好友)
- js高级应用
- ACM学习历程—HDU 3949 XOR(xor高斯消元)
- 我与大话设计模式之规则篇(一)
- DSP28335数组下标越界导致其他全局变量被窜改
- 【栈】顺序栈
- 日经春秋 20151111
- 04 本地部署(单机模式)
- java常见面试题
- OpenGL ES 3.0之Fragment buffer objects(FBO)详解(一)
- hdu More is better