IOS-UITableView編輯模式示例 – iPhone手機開發技術文章 iPhone軟體開發教學課程

概要

本示例實在上篇文章的基礎上的例子修改過來的,主要是簡示瞭UITableView的編輯模式的使用,包括狀態改變、移動行、刪除行。

運行結果

過程概要

見代碼及註釋,不難

主要代碼

h文件

//
//  CityViewController.h
//  NatTab
//
//  Created by God Lin on 14/12/7.
//  Copyright (c) 2014年 arbboter. All rights reserved.
//

#import 

@interface CityViewController : UIViewController 
{
    NSMutableArray* _arrayName;
    UITableView* _tableView;
}

@property (nonatomic, retain) NSMutableArray* _arrayName;
@property (nonatomic, retain) UITableView* _tableView;
@end

m文件

//
//  CityViewController.m
//  NatTab
//
//  Created by God Lin on 14/12/7.
//  Copyright (c) 2014年 arbboter. All rights reserved.
//

#import "CityViewController.h"

@interface CityViewController ()

@end

@implementation CityViewController

@synthesize _arrayName;
@synthesize _tableView;

#pragma UITableViewDelegate
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self._arrayName count];
}

// 插入刪除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self._arrayName removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: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
    }
}

// 移動
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString* fromObj = [self._arrayName objectAtIndex:sourceIndexPath.row];
    [self._arrayName insertObject:fromObj atIndex:destinationIndexPath.row];
    [self._arrayName removeObjectAtIndex:sourceIndexPath.row];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = nil;
    static NSString *CellIdentifier = @"Cell";
    
    cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:CellIdentifier] autorelease];
    }
    
    NSString* iconStr = [NSString stringWithFormat:@"animal_%u.png", arc4random()%17+1];
    cell.imageView.image = [UIImage imageNamed:iconStr];
    cell.textLabel.text = [self._arrayName objectAtIndex:indexPath.row];
    return cell;
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    CGRect viewRect = self.view.frame;
    self._tableView = [[UITableView alloc] initWithFrame:viewRect];
    self._tableView.delegate = (id)self;
    self._tableView.dataSource = self;
    [self.view addSubview:self._tableView];
    
    self._arrayName = [[NSMutableArray alloc] init];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self._tableView setEditing:editing animated:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    int nRow = arc4random()%50+20;
    NSString* str = nil;
    
    
    [self._arrayName removeAllObjects];
    for (int i=0; i<nRow; i++)
    {
        str = [NSString stringWithFormat:@"%@_%c%c%c%c", self.title, arc4random()%26+'a',arc4random()%26+'a',arc4random()%26+'a',arc4random()%26+'a'];
        [self._arrayName addObject:str];
    }
    
    // 刷新數據
    [self._tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [_arrayName release];
    [_tableView release];
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

工程代碼

發佈留言