zoukankan      html  css  js  c++  java
  • UITableView(修改单元格)

    1.删除和插入单元格

      表格一旦进入删除或插入状态,单元格的左边就会出现一个“编辑控件”,这个区域会显示删除控件或插入控件,具体显示那个图标在表视图委托协议的tableView:editingStyleForRowAtIndexPath:方法中设定。

    为了防止用户操作失误,删除过程需要确认。删除是点击Delete按钮,才会成功删除

    删除和插入单元格操作的核心是如下两个方法:表视图委托对象的tableView:editingStyleForRowAtIndexPath:方法和表视图数据源对象的tableView:commitEditingStyle:forRowAtIndexPath:方法

    setEditing:animated:方法设定视图能否进入编辑状态,然后调用委托协议中的tableView:editingStyleForRowAtIndexPath:方法进行单元格编辑图标的设置,当用户删除或修改控件时,委托方法向数据源发出tableView:commitEditingStyle:forRowAtIndexPath:消息实现删除或插入的处理

    @property (strong, nonatomic) IBOutlet UINavigationItem *navigatItem;

    @property (strong, nonatomic) IBOutlet UITextField *txtField;

    @property (weak, nonatomic) IBOutlet UITableView *tableView;

    @property (nonatomic,strong) NSMutableArray *listeams;

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.navigatItem .rightBarButtonItem = self.editButtonItem;

        self.navigatItem.title = @"单元格插入和删除";

        

        self.txtField.hidden = YES;

        self.txtField.delegate = self;

        

        self.tableView.delegate = self ;

        self.tableView.dataSource = self;

        

        self.listeams = [[NSMutableArray alloc]initWithObjects:@"黑龙江",@"吉林", @"辽宁",nil];

    }

    -(void)setEditing:(BOOL)editing animated:(BOOL)animated

    {  

        [super setEditing:editing animated:animated];

        if (editing) {

            self.txtField.hidden = NO;

            

        }else{

            self.txtField.hidden = YES;

        }

        [self.tableView setEditing:editing animated:animated];

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    #pragma mark - UITableViewDataSource

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        return [self.listeams count]+1;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        static NSString *cellIndentifier = @"Cell";

        BOOL b_addCell = (indexPath.row == self.listeams.count);

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];

        if (cell == nil) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];

        }

        if (!b_addCell) {

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

            cell.textLabel.text = [self.listeams objectAtIndex:indexPath.row];

        }else{

            self.txtField.frame = CGRectMake(10, 0, 300, 44);

            self.txtField.text = @"";

            [cell.contentView addSubview:self.txtField];

        }

        return cell ;

    }

    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return YES;

    }

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if (editingStyle == UITableViewCellEditingStyleDelete) {

            [self.listeams removeObjectAtIndex:indexPath.row];

            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];

            [self.tableView reloadData];

        }else{

            [self.listeams insertObject:self.txtField.text atIndex:[self.listeams count]];

            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];

            [self.tableView reloadData];

        }

    }

    #pragma mark - UITableViewDelegate

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if (indexPath.row == self.listeams.count) {

            return UITableViewCellEditingStyleInsert;

        }else{

            return UITableViewCellEditingStyleDelete;

        }

    }

    - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if (indexPath.row == [self.listeams count]) {

            return NO;

        }else{

            return YES;

        }

    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return 50;

    }

    #pragma mark - UITextFieldDelegate

    - (BOOL)textFieldShouldReturn:(UITextField *)textField

    {

        [textField resignFirstResponder];

        return YES;

    }

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

    {

        UITableViewCell *cell = (UITableViewCell*)[[textField superview]superview];

        [self.tableView setContentOffset:CGPointMake(0.0, cell.frame.origin.y) animated:YES];

        return YES;

    }

     移动单元格

      移动单元格时,需要实现数据源的tableView: canMoveRowAtIndexPath:和tableView: moveRowAtIndexPath: toIndexPath:方法

    -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

        return YES;

    }

    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

        NSString *stringToMove = [self.listeams objectAtIndex:sourceIndexPath.row];

        [self.listeams removeObjectAtIndex:sourceIndexPath.row];

        [self.listeams insertObject:stringToMove atIndex:destinationIndexPath.row];

        [self.tableView reloadData];

    }

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return UITableViewCellEditingStyleNone;

    }

  • 相关阅读:
    [洛谷P3674]小清新人渣的本愿
    [洛谷P2698][USACO12MAR]花盆Flowerpot
    [洛谷P4329][COCI2006-2007#1] Bond
    [洛谷P3203][HNOI2010]弹飞绵羊
    [洛谷P1407][国家集训队]稳定婚姻
    [洛谷P3388]【模板】割点(割顶)
    TX2_安装view_team
    tx2的一些系统命令
    tensorflow-cnnn-mnist
    mnist数据集tensorflow实现
  • 原文地址:https://www.cnblogs.com/PJXWang/p/5443271.html
Copyright © 2011-2022 走看看