zoukankan      html  css  js  c++  java
  • UITableView 移动与插入,

     转:http://blog.csdn.net/smile3670/article/details/7904844

    移动关键代码

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

       returnUITableViewCellEditingStyleNone;

    }

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

       returnYES;

    }

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

        NSInteger sourceRow= [sourceIndexPath row];

        NSInteger destinationRow = [destinationIndexPath row];

        id object = [[self.arrayobjectAtIndex:sourceRow]retain];

        [self.arrayremoveObjectAtIndex:sourceRow];

        [self.arrayinsertObject:objectatIndex:destinationRow];

        [object release];

    }

     

    在UITableView中插入或者删除指定的行(或者节)使用的是如下几个API:

    insertRowsAtIndexPath: withRowAnimation: 在指定位置插入行

    deleteRowsAtIndexPath: withRowAnimation: 删除指定行

    insertSections: withRowAnimation: 在指定位置插入节

    deleteSections: withRowAnimation: 删除指定节

    调用以上API之前,必须先调用beginUpdates,插入/删除数据完成后再调用endUpdates。

    -(IBAction)addRows:(id)sender{

       NSMutableArray *indexPaths = [[NSMutableArrayallocinit];

        for (int i=0; i<3; i++) {

            NSString *s = [[NSString alloc] initWithFormat:@”hello %d”,i];

            [datas addObject:s];

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];

            [indexPaths addObject: indexPath];

        }

        [self.tableViewbeginUpdates];

        [self.tableViewinsertRowsAtIndexPaths:indexPathswithRowAnimation:UITableViewScrollPositionNone];

        [self.tableViewendUpdates];

    }

    -(IBAction)delRows:(id)sender{

       NSMutableArray *indexPaths = [[NSMutableArrayallocinit];

        [datas removeObjectAtIndex:0];

        [indexPathsaddObject:[NSIndexPathindexPathForRow:0inSection:0]];

        [self.tableViewbeginUpdates];

        [self.tableViewdeleteRowsAtIndexPaths:indexPathswithRowAnimation:UITableViewRowAnimationNone];

        [self.tableViewendUpdates];

    }

        //更新

        NSMutableArray *indexPaths = [[NSMutableArray allocinit];

        [datas removeObjectAtIndex:0];

        [datas insertObject:@"AAA" atIndex:0];

        [indexPaths addObject:[NSIndexPath indexPathForRow:0 inSection:0]];

        [self.tableView reloadRowsAtIndexPaths:indexPathswithRowAnimation:UITableViewRowAnimationFade];

        [indexPaths release];



    需要注意的是,调用insert函数时,需保证数据源添加的记录数要与你想插入的行的总数一致,如上面的例子中,想要插入的记录有3条,插入位置分别为1,2,3,则对应的indexpPaths数组的元素总数为3,数组元素为一个NSIndexPath对象,通过它我们指定了记录的插入位置。删除数据也是相同的道理。

    局部更新三部曲,

        [self.tableViewbeginUpdates];

        [self.tableViewinsertRowsAtIndexPaths:[NSArrayarrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

        [self.tableViewendUpdates];

  • 相关阅读:
    mvc使用model进行数据的增加修改的方法
    c#导出word在iis部署上报异常
    做个转圈圈的咚咚
    VS2008中AJAX的部署问题(工具箱中无AJAX Extensions选项卡)
    关于 AjaxControlToolkit requires ASP.NET Ajax 4.0 scripts. 错误
    ASP.NET关于继承DropDownList的自定义DDL控件
    线性表顺序表示的C#实现(参考数据结构(C语言版))
    WORD2003出现的乱码
    线性表链式表示的C#实现(参考数据结构(C语言版))
    有错误先找自己的原因(若你百度不出为什么vista开网页慢,可以来试试这方法)
  • 原文地址:https://www.cnblogs.com/guligei/p/3483468.html
Copyright © 2011-2022 走看看