IOS-UITableView編輯模式樣本

來源:互聯網
上載者:User

IOS-UITableView編輯模式樣本
概要

本樣本實在上篇文章的基礎上的例子修改過來的,主要是簡示了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

工程代碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.