tableView 的cell功能操作方法

 

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里给你一个简单的示例代码,用于演示 UITableView 在数据源发生变化时如何自动更新: ``` // 定义一个数组用于存储数据 NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:@"第一行", @"第二行", @"第三行", nil]; // 实现 UITableView 的数据源法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // 获取对应的数据 NSString *data = [dataArray objectAtIndex:indexPath.row]; // 更新 UITableViewCell 的内容 cell.textLabel.text = data; return cell; } // 在某个操作后更新数据源并刷新 UITableView [dataArray removeObjectAtIndex:1]; [tableView reloadData]; ``` 在上面的代码中,我们首先定义了一个数组 `dataArray`,用于存储数据。然后在 UITableView 的数据源法中,我们根据当前的 indexPath 来获取对应的数据,并使用该数据来更新 UITableViewCell 的内容。 最后,在某个操作(例如删除第二行数据)后,我们需要更新数据源 `dataArray`,并调用 UITableView 的 `reloadData` 法来刷新 UITableView。这样,UITableView 就会自动更新显示的内容,以反映最新的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值