UITableView刪除添加和移動

來源:互聯網
上載者:User

標籤:

#import "RootTableViewController.h"@interface RootTableViewController ()@property (nonatomic, strong) NSMutableArray *allDataArray;@property (nonatomic, assign) UITableViewCellEditingStyle style;@end@implementation RootTableViewController- (void)viewDidLoad {    [super viewDidLoad];            self.view.backgroundColor = [UIColor lightGrayColor];            // 設定導覽列    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];    self.title = @"尹浩";    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];            // 處理資料    [self handleData];            // 註冊tableView    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];            // 添加右按鈕    UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(rightBarButtonItemClick:)];        UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(moveClick:)];        self.navigationItem.rightBarButtonItems = @[button1, button2];            // 添加左按鈕    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftBarButtonItemClick:)];}// 處理資料- (void)handleData {        // 1.初始化大數組    self.allDataArray = [NSMutableArray array];        // 2.定義三個數存放每一組學生的姓名    NSMutableArray *array1 = @[@"泰隆", @"刀妹", @"卡牌大師", @"提莫", @"艾希", @"蠻王"].mutableCopy;        NSMutableArray *array2 = @[@"蓋聶", @"衛莊", @"天明", @"少羽", @"高月"].mutableCopy;        NSMutableArray *array3 = @[@"尹浩", @"尹笑", @"尹雙浩", @"尹冬冬", @"尹句號"].mutableCopy;        // 3.將所有學生存放到大數組中    [self.allDataArray addObject:array1];    [self.allDataArray addObject:array2];    [self.allDataArray addObject:array3];    }#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {        return self.allDataArray.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {        return [self.allDataArray[section] count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];        // 設定資料    NSArray *array = [self.allDataArray objectAtIndex:indexPath.section];    cell.textLabel.text = array[indexPath.row];            return cell;}// 設定行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {        return 60;}// 取消選中狀態- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {        [tableView deselectRowAtIndexPath:indexPath animated:YES];}#pragma mark - 編輯(刪除和添加)// 右按鈕點擊事件- (void)rightBarButtonItemClick:(UIBarButtonItem *)sender {        self.style = UITableViewCellEditingStyleDelete;        // 讓cell處於編輯狀態    [self.tableView setEditing:!self.tableView.editing animated:YES];    }// 左按鈕點擊事件- (void)leftBarButtonItemClick:(UIBarButtonItem *)sender {        self.style = UITableViewCellEditingStyleInsert;        [self.tableView setEditing:!self.tableView.editing animated:YES];}// 指定哪些cell可以編輯- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {        if (indexPath.section == 0 || indexPath.section == 1) {        return YES;    }        return NO;}// 設定編輯樣式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {        return self.style;}// 完成編輯- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {        if (editingStyle == UITableViewCellEditingStyleInsert) {                [self.allDataArray[indexPath.section] insertObject:@"星魂" atIndex:indexPath.row + 1];                NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];        [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];            } else if (editingStyle == UITableViewCellEditingStyleDelete) {                [self.allDataArray[indexPath.section] removeObjectAtIndex:indexPath.row];                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];    }}#pragma mark - 移動// 讓cell處於編輯狀態- (void)moveClick:(UIBarButtonItem *)sender {        [self.tableView setEditing:!self.tableView.editing animated:YES];}// 設定哪些cell可以移動- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {        return YES;}// 開始移動- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {        // 擷取需要修改的資料    NSString *sourceName = [self.allDataArray[sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];        // 先將資料從當前位置移除    [self.allDataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];        // 再將資料插入到對應的位置    [self.allDataArray[destinationIndexPath.section] insertObject:sourceName atIndex:destinationIndexPath.row];    }// 防止隨意移動- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {        if (sourceIndexPath.section == proposedDestinationIndexPath.section) {        return proposedDestinationIndexPath;    } else {        return sourceIndexPath;    }}@end

 

UITableView刪除添加和移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.