(三)UITabBar and UINavigationController基础教程之UITableView的插入删除移动

之前我们实现了页面切换以及UITextField键盘隐藏及防止键盘遮挡


(一)UITabBar and UINavigationController基础教程之切换页面http://blog.csdn.net/zhangyankan/article/details/12833619

(二)UITabBar and UINavigationController基础教程之UITextField键盘隐藏及防止键盘遮挡http://blog.csdn.net/zhangyankan/article/details/12842205


今天我们来实现一下简单的UITableView的插入删除移动


首先我们在.m文件中


#import <UIKit/UIKit.h>
#import "JBaseViewController.h"

@interface JThirdViewController : UITableViewController<UITableViewDataSource,UITableViewDataSource>

@property (strong, nonatomic) NSMutableArray *list;
@end

接下来我们在.h文件中实现


#import "JThirdViewController.h"

@interface JThirdViewController ()

@end

@implementation JThirdViewController
@synthesize list;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[self navigationItem]setRightBarButtonItem:[self editButtonItem]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"美国", @"菲律宾",@"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",@"日本" , nil];
    self.list = array;
    
    self.navigationItem.title = @"列表";
    
    self.tableView.editing = YES;
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell==nil) {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
//        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
//        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
//        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]autorelease];
    }
    
    // Configure the cell...
    NSUInteger row = [indexPath row];
    cell.textLabel.text=[self.list objectAtIndex:row];
    
    UIImage *image = [UIImage imageNamed:@"add_file.png"];
    cell.imageView.image = image;
    UIImage *highLighedImage = [UIImage imageNamed:@"add_list.png"];
    cell.imageView.highlightedImage = highLighedImage;
    
    cell.detailTextLabel.text =@"haha";
    return cell;
}

#pragma mark - Table view delegate

//选中效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSString *rowString = [self.list objectAtIndex:[indexPath row]];
//    UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//    [alter show];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
//        cell.accessoryType = UITableViewCellAccessoryDetailButton;
//        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

//删除 插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.list removeObjectAtIndex:row];
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:
         UITableViewRowAnimationAutomatic
         //UITableViewRowAnimationBottom
         //UITableViewRowAnimationFade
         //UITableViewRowAnimationLeft
         //UITableViewRowAnimationMiddle
         //UITableViewRowAnimationNone
         //UITableViewRowAnimationRight
         //UITableViewRowAnimationTop
         ];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath, nil];
        [self.list insertObject:@"aaaa" atIndex:row];
        [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
    }
    
}

//移动
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return UITableViewCellEditingStyleNone;
    return UITableViewCellEditingStyleInsert;
}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSUInteger fromRow = [sourceIndexPath row];
    NSUInteger toRow = [destinationIndexPath row];
    
    id object = [self.list objectAtIndex:fromRow];
    [self.list removeObjectAtIndex:fromRow];
    [self.list insertObject:object atIndex:toRow];
}


@end

       


具体的介绍可以参考我之前发过的


iOS学习之TableView的简单使用http://blog.csdn.net/zhangyankan/article/details/12776961

iOS学习之分段Table View的使用(Grouped样式表格)http://blog.csdn.net/zhangyankan/article/details/12776965

iOS学习之UITableView中Cell的操作http://blog.csdn.net/zhangyankan/article/details/12776975

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Objective-C 中,你可以通过以下步骤在分栏控制器的按钮上插入图片并更改图片的大小: 1. 首先,准备好你想要使用的图片,并将其添加到项目的资源文件夹中。 2. 打开你的分栏控制器的视图控制器文件,这通常是 AppDelegate.m 或者你自定义的分栏控制器类。 3. 在视图控制器文件中,找到初始化分栏控制器的代码。这通常是在 `application:didFinishLaunchingWithOptions:` 方法中。 4. 找到你想要添加图片的按钮对象。你可以使用 `UITabBarItem` 类来访问按钮对象。 5. 创建一个 `UIImage` 对象,其中包含你想要使用的图片。你可以使用 `imageNamed:` 方法从资源文件夹中加载图片,如下所示: ```objc UIImage *image = [UIImage imageNamed:@"your_image_name"]; ``` 确保将 "your_image_name" 替换为你实际的图片文件名。 6. 使用 `setImage:` 方法将图片设置为按钮的图像,如下所示: ```objc [button setImage:image]; ``` 确保将 "button" 替换为你实际的按钮对象。 7. 如果需要调整图片的大小,你可以使用 `UIImage` 类的 `imageWithCGImage:scale:orientation:` 方法来创建一个新的调整大小的图片对象。例如,如果你想将图片大小调整为宽度为30个点,高度为30个点: ```objc UIImage *resizedImage = [UIImage imageWithCGImage:image.CGImage scale:2.0 orientation:image.imageOrientation]; ``` 确保将 "image" 替换为你之前创建的图片对象,并根据需要调整 `scale` 的值。 8. 最后,使用调整大小的图片对象设置按钮的图像,如下所示: ```objc [button setImage:resizedImage]; ``` 这样,你就可以在分栏控制器的按钮上插入图片并更改图片的大小了。记得根据你的项目结构和代码适当修改这些步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值