标签:ios ios开发 objective-c uiviewcontroller uitableviewcell
插入和删除时序:
client: setEditing: animated: -----> 设定进入表视图
表视图---->委托: (<UITableViewDelegate>)tableView:editingStyleForRowAtIndexPath:方法进行单元格编辑图标的设置
方法进行单元格编辑图标的设置
表视图---->数据源:(<UITableViewDataSource>)tableView:commiEditingStyle:forRowAtIndexPath消息实现删除或者插入的处理
这里由点击insert or delete控件来实现
删除和增加单元格要通过[self.tableView reloadData]来重新加载表视图数!!
移动时序:
client:setEditing:animated:----->设定进入表视图
表视图--->委托:(<UITableViewDelegate>)tableView:editingStyleForRowAtIndexPath:方法进行单元格编辑图标的设置
方法进行单元格编辑图标的设置
表视图---->数据源:(<UITableViewDataSource>)tableView:canMoveRowAtIndexPath 消息允许数据源移动单元格
表视图---->委托:(<UITableViewDelegate>)tableView:moveRowAtIndexPath:toIndexPath:方法对listTeams重新排序
党用户拖动排序控件来实现。
ViewController.h:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) IBOutlet UINavigationItem *navgationItem; @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (strong, nonatomic) IBOutlet UITextField *txtField; @property (nonatomic, strong) NSMutableArray *listTeams; @end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航栏
// editButtonItem在视图控制器中已经定义好,self.editButtonItem可以获取指针
// rightBarbuttonitem为导航栏右边的按钮
self.navgationItem.rightBarButtonItem = self.editButtonItem;
self.navgationItem.title = @"单元格插入和删除";
// 设置单元格文本框
self.txtField.hidden = YES;
self.txtField.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.
}
#pragma mark -- UIViewController 生命周期方法,用于响应视图编辑状态变化,判断是否能够进入编辑状态!
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated: YES];
if (editing) {
self.txtField.hidden = NO;
}
else
{
self.txtField.hidden = YES;
}
}
#pragma mark -- UITableViewDataSource 数据源协议方法
- (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;
// 判断是否为需要插入的单元格
if (indexPath.row == self.listTeams.count) {
b_addCell = YES;
}
else
{
b_addCell = NO;
}
// 建立重用Cell
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]; // 获取text
}
else{
// 新插入的单元格
// 只有进行插入操作时候,才能初始化到这个部分!
self.txtField.frame = CGRectMake(10, 0, 300, 44);
self.txtField.text = @"";
[cell.contentView addSubview:self.txtField]; // 实现文本框的注入
}
return cell;
}
#pragma mark -- UITableViewDelegate 委托协议方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 编辑图标设定
// 是删除还是增加
if (indexPath.row == [self.listTeams count]) {
return UITableViewCellEditingStyleInsert;
}
else{
return UITableViewCellEditingStyleNone;//重排序控件同时存在。
//return UITableViewCellEditingStyleDelete;
}
}
- (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)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 先获取目标
NSString *stringToMove = [self.listTeams objectAtIndex:sourceIndexPath.row];
// 从目标位置删除
[self.listTeams removeObjectAtIndex:sourceIndexPath.row];
// 再目标位置插入数据
[self.listTeams insertObject:stringToMove atIndex:destinationIndexPath.row];
}
#pragma mark -- UITableViewDataSource协议方法
- (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) {
// 在最末位添加到listTeams
// 先添加数据,然后再显示
[self.listTeams insertObject:self.txtField.text atIndex:[self.listTeams count]];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// 重新加载在表格里的数据
[self.tableView reloadData];
}
}
#pragma mark -- UITextFieldDelegate 委托方法,1.关闭键盘 2.避免键盘遮挡文本框
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
// 回收键盘
[textField resignFirstResponder];
return YES;
}
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell *) [[textField superview] superview];
[self.tableView setContentOffset:CGPointMake(0.0, cell.frame.origin.y) animated:YES];
}
@end
标签:ios ios开发 objective-c uiviewcontroller uitableviewcell
原文地址:http://blog.csdn.net/liyakun1990/article/details/40512287