标签:uitableview
本示例实在上篇文章的基础上的例子修改过来的,主要是简示了UITableView的编辑模式的使用,包括状态改变、移动行、删除行。
见代码及注释,不难
// // CityViewController.h // NatTab // // Created by God Lin on 14/12/7. // Copyright (c) 2014年 arbboter. All rights reserved. // #import <UIKit/UIKit.h> @interface CityViewController : UIViewController <UITabBarDelegate, UITableViewDataSource> { NSMutableArray* _arrayName; UITableView* _tableView; } @property (nonatomic, retain) NSMutableArray* _arrayName; @property (nonatomic, retain) UITableView* _tableView; @end
// // 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
标签:uitableview
原文地址:http://blog.csdn.net/arbboter/article/details/41794255