自定义UITableViewCell上的delete按钮

滑动列表行(UITableViewCell)出现删除按钮时,默认是英文“delete”,这份代码片段能够将“delete”变成中文”删除“,甚至可以自定义删除按钮的形状。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//通过UITableViewDelegate方法可以实现删除 tableview中某一行
//滑动删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{  
    NSUInteger row = [indexPath row];
    [bookInforemoveObjectAtIndex:row];//bookInfo为当前table中显示的array
    [tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
 
/*此时删除按钮为Delete,如果想显示为“删除” 中文的话,则需要实现
UITableViewDelegate中的- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath方法*/
 
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{   
    return @"删除";   
}   
//或者,最简单的方式,将plist中的Localization native development region改为China即可
 
//这只是让默认的Delete按钮显示成了中文的删除按钮而已,如果想将这个删除按钮换成其他图片形式的,怎么办呢?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
{   
    static NSString *RootViewControllerCell = @"RootViewControllerCell";   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RootViewControllerCell];   
    if(cell == nil)   
    {   
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:RootViewControllerCell]autorelease];   
             
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];   
        [button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];   
        [button setFrame:CGRectMake(280, 10, 30, 30)];   
        [button addTarget:self action:@selector(del:) forControlEvents:UIControlEventTouchUpInside];   
        [cell.contentView addSubview:button];          
    }   
         
    cell.textLabel.text = [array objectAtIndex:[indexPath row]];   
    cell.tag = [indexPath row];   
         
    NSArray *subviews = [cell.contentView subviews];   
    for(id view in subviews)   
    {   
        if([view isKindOfClass:[UIButton class]])   
        {   
            [view setTag:[indexPath row]];   
            [cell.contentView bringSubviewToFront:view];   
        }   
    }   
    return cell;   
}   
     
-(void)del:(UIButton *)button   
{   
    NSArray *visiblecells = [self.table visibleCells];   
    for(UITableViewCell *cell in visiblecells)   
    {   
        if(cell.tag == button.tag)   
        {   
            [array removeObjectAtIndex:[cell tag]];   
            [table reloadData];   
            break;   
        }   
    }   
}

自定义UITableViewCell 的delete按钮的更多相关文章

  1. 如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row

    在自定义UITableViewCell中创建了一个按钮. 想在点击该按钮时知道该按钮所在的cell在TableView中的行数.就是cell的 indexPath.row两种方法都很好.-(IBAct ...

  2. 自定义 UITableViewCell 的 accessory 样式

    对于 UITableViewCell 而言,其 accessoryType属性有4种取值: UITableViewCellAccessoryNone, UITableViewCellAccessory ...

  3. UITableViewCell上的按钮点击事件处理

    转自:  http://www.aichengxu.com/view/42871 UITableViewCell上的按钮点击事件处理,有需要的朋友可以参考下. 今天突然做项目的时候,又遇到处理自定义的 ...

  4. 自定义UITableViewCell实现左滑动多菜单功能LeftSwipe

    今天愚人节,小伙们,愚人节快乐! 实现一个小功能,滑动菜单,显示隐藏的功能菜单, 先上图:                       这里尝试用了下使用三个方式来实现了这个功能: 1.使用自定义UI ...

  5. 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)

    自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...

  6. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

  7. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  8. 新手教程之使用Xib自定义UITableViewCell

    新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...

  9. 【转】iOS 通过xib自定义UITableViewCell【原创】

    原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...

随机推荐

  1. IEEE Bigger系列题解

    Bigger系列题解 Bigger Python 坑点在于要高精度以及表达式求值,用java写可以很容易避免高精度问题 然后这道题就可以AC了 代码 import java.io.*; import ...

  2. UVA 11947 Cancer or Scorpio 水题

    Cancer or Scorpio Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://uva.onlinejudge.org/index.php? ...

  3. ZOJ 3626 Treasure Hunt I 树上DP

    E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...

  4. ROS知识(9)----安装Turtlebot2和远程控制Turtlebot2

    安装turtlebot2,场景为:turtlebot2上搭载着一台电脑主机A,该电脑作为主机Master,有自带的电源和3D传感器,roscore在该台机器上启动.pc电脑远程连接A,和A通讯,pc不 ...

  5. IO流-批量修改文件名称案例

    /* *   源文件名:   桌面-我们今天学习IO流了哈哈哈哈-001.jpg *   修改后文件名:  桌面-000x.jpg */public class File_listFiles_upda ...

  6. mongodb exception in initAndListen: 12596 old lock file, terminating解决方法

    错误信息如下: exception old lock file, terminating 解决方法 .删除data目录中的.lock文件 .mongod.exe --repair .启动mongod就 ...

  7. 平时常用的Visual Studio操作技巧,持续更新中……

    移除未使用的命名空间--方法1:右键--"组织using"--"移除未使用的using"--方法2:Shift+F10--"O"-" ...

  8. PL/SQL的下载与安装

    注意,为了更好的通用性,最好安装32位的. 下载地址为:https://www.allroundautomations.com/bodyplsqldevreg.html 当前最新正式版本为PL/SQL ...

  9. Spring+Quartz的版本问题

    使用Spring配置管理Quartz的时候会遇到下面的异常: Caused by: java.lang.IncompatibleClassChangeError: class org.springfr ...

  10. 使用addChildViewController手动控制UIViewController的切换

    addChildViewController If the new child view controller is already the child of a container view con ...