关闭

tableView 的cell功能操作方法

标签: tableView 的cell功能操作方
153人阅读 评论(0) 收藏 举报
分类:
 

tableView 的cell功能操作方法 

 29人阅读 评论(0) 收藏 举报

目录(?)[+]

批量操作方法

<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">// 允许在编辑模式进行多选操作</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.tableView</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.allowsMultipleSelectionDuringEditing</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;</code>

图1
<code class="objectivec">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">IBAction</span>)remove {

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 获得所有被选中的行</span>

    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSArray</span> *indexPaths = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.tableView</span> indexPathsForSelectedRows];

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 便利所有的行号</span>

    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableArray</span> *deletedDeals = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableArray</span> array];

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *path <span class="hljs-keyword" style="color: rgb(0, 0, 136);">in</span> indexPaths) {

        [deletedDeals addObject:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.deals</span>[path<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>]];

    }


    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 删除模型数据</span>

    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.deals</span> removeObjectsInArray:deletedDeals];


    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 刷新表格  一定要刷新数据</span>

    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.tableView</span> deleteRowsAtIndexPaths:indexPaths withRowAnimation:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowAnimationTop</span>];

}</code>
---------------------------华丽的分割线------------------------
<code class="objectivec">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)tableView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableView</span> *)tableView didSelectRowAtIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath {

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 取消选中</span>

    [tableView deselectRowAtIndexPath:indexPath animated:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];

}</code>
开启编辑
<code class="objectivec">[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.tableView</span> setEditing:!<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.tableView</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.editing</span> animated:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 有动画</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">/**

 *  返回indexPath对应的编辑样式

 */</span>

- (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellEditingStyle</span>)tableView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableView</span> *)tableView editingStyleForRowAtIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath {

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (tableView<span class="hljs-variable" style="color: rgb(102, 0, 102);">.isEditing</span>) { <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 如果是编辑模式,则返回插入操作</span>

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellEditingStyleInsert</span>;

    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellEditingStyleDelete</span>;

}

<span class="hljs-comment" style="color: rgb(136, 0, 0);">//哪几行可以编辑</span>

- (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)tableView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableView</span> *)tableView canEditRowAtIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath{

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span> > <span class="hljs-number" style="color: rgb(0, 102, 102);">4</span>) {

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>;

    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;

}</code>
---------------------------华丽的分割线------------------------

移动cell的方法


图2
<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">//移动cell方法</span>

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)tableView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableView</span> *)tableView moveRowAtIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)sourceIndexPath toIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)destinationIndexPath{

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//   如果原始位置与目标位置不相等   移动模型数组里的数据</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (sourceIndexPath != destinationIndexPath) {

        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//取出原始位置移动的那一行的模型</span>

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">id</span> object = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.contacts</span> objectAtIndex:sourceIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>];

        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//删除  数组中原始索引对应的模型    object</span>

        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.contacts</span> removeObjectAtIndex:sourceIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>];

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>(destinationIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span> > [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.contacts</span> count]) { <span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果目标位置大于数组的数量由于之前移除了一个模型  所以这里刚好模型数组的数量也减了1</span>

            <span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果目标位置大于数组数  就把模型直接添加到最后一个位置</span>

            [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.contacts</span> addObject:object];

        }

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {

            <span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果目标位置小于数组数  就把模型数据插入到数组对应目标位置的地方</span>

            [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.contacts</span> insertObject:object atIndex:destinationIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>];

        }

    }


    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//更新数据</span>

    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSKeyedArchiver</span> archiveRootObject:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.contacts</span> toFile:FilePath];


}</code>
<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">//哪几行可以移动(可移动的行数小于等于可编辑的行数)</span>

- (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)tableView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableView</span> *)tableView canMoveRowAtIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath{

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span> > <span class="hljs-number" style="color: rgb(0, 102, 102);">5</span>) {

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>;

    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;

}</code>
<code class="objectivec">- (nullable <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSArray</span><<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowAction</span> *> *)tableView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableView</span> *)tableView editActionsForRowAtIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath {


        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowAction</span> *action = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowAction</span> rowActionWithStyle:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowActionStyleDefault</span> title:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"删除"</span> handler:^(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowAction</span> * _Nonnullaction, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> * _NonnullindexPath) {


                <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"已删除"</span>);

            }];w


        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowAction</span> *actionSign = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowAction</span> rowActionWithStyle:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowActionStyleNormal</span> title:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"关注微信"</span> handler:^(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewRowAction</span> * _Nonnullaction, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> * _NonnullindexPath) {


                <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"标记关注"</span>);

            }];

        actionSign<span class="hljs-variable" style="color: rgb(102, 0, 102);">.backgroundColor</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIColor</span> blueColor];


        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> @[action,actionSign];

}</code>

全选按钮操作

<code class="objectivec">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)allSelect:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIButton</span>*)sender{
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSArray</span> *anArrayOfIndexPath = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSArray</span> arrayWithArray:[table indexPathsForVisibleRows]];
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; i < [anArrayOfIndexPath count]; i++) {
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *indexPath= [anArrayOfIndexPath objectAtIndex:i];
        LTableViewCell *cell = (LTableViewCell*)[table cellForRowAtIndexPath:indexPath];
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSUInteger</span> row = [indexPath row];
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"%lu"</span>,(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">unsigned</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">long</span>)row);
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *dic = [contacts objectAtIndex:row];
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> ([[[(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIButton</span>*)sender titleLabel] text] isEqualToString:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"全选"</span>]) {
            [dic setObject:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"YES"</span> forKey:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"checked"</span>];
            [cell setChecked:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];
        }<span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {
            [dic setObject:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"NO"</span> forKey:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"checked"</span>];
            [cell setChecked:<span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>];
        }
    }
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> ([[[(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIButton</span>*)sender titleLabel] text] isEqualToString:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"全选"</span>]){
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDictionary</span> *dic <span class="hljs-keyword" style="color: rgb(0, 0, 136);">in</span> contacts) {
            [dic setValue:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"YES"</span> forKey:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"checked"</span>];
        }
        [(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIButton</span>*)sender setTitle:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"取消"</span> forState:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIControlStateNormal</span>];
    }<span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDictionary</span> *dic <span class="hljs-keyword" style="color: rgb(0, 0, 136);">in</span> contacts) {
            [dic setValue:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"NO"</span> forKey:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"checked"</span>];
        }
        [(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIButton</span>*)sender setTitle:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"全选"</span> forState:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIControlStateNormal</span>];
    }
}</code>

一旦当tabelView的cell被选中的时候 cell的子控件都会进入高亮状态,通过高亮状态可以设置cell子控件的图片和文字

<code class="objectivec"> <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 设置高亮图片(cell选中 -> cell.imageView.highlighted = YES -> cell.imageView显示highlightedImage这个图片)   </span>
cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.imageView</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.highlightedImage</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIImage</span> imageNamed:c<span class="hljs-variable" style="color: rgb(102, 0, 102);">.highlighted_icon</span>];      
 <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 设置label高亮时的文字颜色</span>
cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.textLabel</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.highlightedTextColor</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIColor</span> redColor];</code>


文/枫林晚_VG(简书作者)
原文链接:http://www.jianshu.com/p/54b9123ae42f
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:29033次
    • 积分:1084
    • 等级:
    • 排名:千里之外
    • 原创:75篇
    • 转载:58篇
    • 译文:0篇
    • 评论:0条
    文章分类