温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

UITableViewCell的编辑,移动,添加或者删除

发布时间:2020-07-23 06:06:42 来源:网络 阅读:4917 作者:Im刘亚芳 栏目:开发技术

类和文件

AppDelegate.m

#import "AppDelegate.h"
#import "MainViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    MainViewController *mainVC = [[MainViewController alloc] init];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
    self.window.rootViewController  = navVC;
    [mainVC release];
    [navVC release];
    [_window release];
    
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end


MainViewController.h

#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@property (nonatomic , retain)NSMutableArray *array;
@end


MainViewController.m

#import "MainViewController.h"
@interface MainViewController ()<UITableViewDataSource , UITableViewDelegate>
@property (nonatomic , retain)UITableView *tableView;  //属性
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.array = [NSMutableArray arrayWithObjects:@"郑浩", @"吴月", @"杨雪", @"彭宇峰", @"高军全", @"胡寒予", @"陈腾飞", @"赵相庆", @"于青池", @"任庆民", @"谢菊花", @"吕俊廷", @"黄舜", @"翟英鹏", @"孟兆旭", @"王栋", @"卞成龙", @"张佳美", @"赵麟嵘", @"南国林", @"王俊", @"刘福彧", @"刘亚芳", nil];
    }
    return self;
}
- (void)dealloc
{
    [_tableView release];
    [_array release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"通讯录";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView ];
    [self.tableView release];
    
    //开启tableView的编辑模式
//    [tableView setEditing:YES animated:YES];
    //系统提供的一个编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    //是否能够被编辑
//    if (indexPath.row == 0) {
//        return YES;
//    }
//    return NO;
//}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //1.获取到要移动的数据源
    NSString *str = [[self.array objectAtIndex:sourceIndexPath.row] retain];
    //2.讲数据从原来的位置移除掉
    [self.array removeObjectAtIndex: sourceIndexPath.row];
    //3.把数据放到最终的位置
    [self.array insertObject:str atIndex:destinationIndexPath.row];
    //4.内存管理
    [str release];
}
//点击编辑按钮,系统会调用这个方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    NSLog(@"edtiting:%d,animated:%d",editing,animated);
    //利用系统的编辑按钮  改变tableView的编辑状态
    [self.tableView setEditing:editing animated:animated];
}
//改变cell的编辑样式(插入/删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleInsert;
    
}
//点击delete按钮的时候,系统调用协议方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //对进行测操作的判断(删除/添加)
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        //在删除一个cell之前,一定要删除数据源里面相应的内容
        [self.array removeObjectAtIndex:indexPath.row];
        //但是删除的时候,写删除相应cell  参数1;要删除的indexPath组成的数组  参数2:要删除row时候展现动画效果
        NSArray *array = [NSArray arrayWithObjects:indexPath, nil];
        [tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationRight];
    }
    else if (UITableViewCellEditingStyleInsert == editingStyle){
        NSString *name = @"ssss";
        [self.array addObject:name];
         NSArray *array = [NSArray arrayWithObjects:indexPath, nil];
        [tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationLeft];
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"aaa";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
        
    }
    cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = @"全班人名";
    return cell;
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSLog(@"%@", [self.array objectAtIndex:indexPath.row]);
//}
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 5;
//}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return [NSString stringWithFormat:@"section:%d",section];
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 25;
//}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI