UITableView 删除和添加单元格

实现的效果:,           

点击“+”,会生成新的单元格,点击edit,会变为右图效果,点击可删除。

 

源码:

/*
 Erica Sadun, http://ericasadun.com
 iPhone Developer's Cookbook, 3.0 Edition
 BSD License, Use at your own risk
 */

#import <UIKit/UIKit.h>

#define COOKBOOK_PURPLE_COLOR    [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR)     [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
#define SYSBARBUTTON(ITEM, SELECTOR) [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:ITEM target:self action:SELECTOR] autorelease]

@interface TableListViewController : UITableViewController
{
    int count;
    NSMutableArray *items;
}
@property (assign) int count;
@property (retain) NSMutableArray *items;
@end

@implementation TableListViewController
@synthesize count;
@synthesize items;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView 
{ 
    return 1; 
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 
{
    return self.items.count;
}

- (void) setBarButtonItems
{
    self.navigationItem.leftBarButtonItem = SYSBARBUTTON(UIBarButtonSystemItemAdd, @selector(addItem:));
    
    if (self.tableView.isEditing)
        self.navigationItem.rightBarButtonItem = SYSBARBUTTON(UIBarButtonSystemItemDone, @selector(leaveEditMode));
    else
        self.navigationItem.rightBarButtonItem = self.items.count ? SYSBARBUTTON(UIBarButtonSystemItemEdit, @selector(enterEditMode)) : nil;
}

- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return a dequeued cell
    UITableViewCellStyle style =  UITableViewCellStyleDefault;
    UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:@"BaseCell"];
    if (!cell) 
        cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell"] autorelease];
    cell.textLabel.text = [items objectAtIndex:indexPath.row];
    return cell;
}

- (void) updateItemAtIndexPath: (NSIndexPath *) indexPath withString: (NSString *) string
{
    // You cannot insert a nil item. Passing nil is a delete request.
    if (!string) 
        [self.items removeObjectAtIndex:indexPath.row];
    else 
        [self.items insertObject:string atIndex:indexPath.row];

    [self.tableView reloadData];
    [self setBarButtonItems];
}

//点击添加后的响应
- (void) addItem: (id) sender
{
    // add a new item
    NSIndexPath *newPath = [NSIndexPath indexPathForRow:self.items.count inSection:0];
    NSString *newTitle = [NSString stringWithFormat:@"Item %d", count++];
    [self updateItemAtIndexPath:newPath withString:newTitle];
}

//点击删除按钮后的响应
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // delete item
    [self updateItemAtIndexPath:indexPath withString:nil];
}


-(void)enterEditMode
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
    [self.tableView setEditing:YES animated:YES];
    [self setBarButtonItems];
}

-(void)leaveEditMode
{
    [self.tableView setEditing:NO animated:YES];
    [self setBarButtonItems];
}

- (void) loadView
{
    [super loadView];
    count = 1;
    self.items = [NSMutableArray array];
    [self setBarButtonItems];
}
@end

@interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
@end

@implementation TestBedAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application 
{    
    
    TableListViewController *tlvc = [[TableListViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tlvc];
    nav.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
    
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [window addSubview:nav.view];
    [window makeKeyAndVisible];
}
@end

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");
    [pool release];
    return retVal;
}
View Code

这里的添加是另外自己写的响应,但是看源码,发现其实,删除、添加的响应都是

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"commitEditingStyle StyleDelete called!");
        // Delete the row from the data source
       // [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
       
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

 

转载于:https://www.cnblogs.com/wyqfighting/p/3172044.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值