博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Adding a custom image to a UITableView delete button
阅读量:5021 次
发布时间:2019-06-12

本文共 3821 字,大约阅读时间需要 12 分钟。

Apple makes it difficult if not imposible to customize elements such as a UITableView’s delete button. Well today I found a way to by pass all the limitations and add a custom image to a delete button. Now I first want to warn you, this is not an official way of doing this, we will be adding a UIImageView over the delete button’s subview. So that being said, lets get started!

The first thing we need to do is set up a UITableView.

In your viewcontroller.m viewdidload method add the following code:

UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; [table setDataSource:self];[table setDelegate:self];[self.view addSubview:table];[table setEditing:TRUE animated:TRUE];[table release];

Now add the table view delegate methods to your viewcontroller.m:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {	return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];	if (cell == nil) {		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];		}		cell.textLabel.text = @"txt";		return cell;}	- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{	[tableView deselectRowAtIndexPath:indexPath animated:YES];}

And voila! You should now have a tableview that automatically enters edit mode on launch. But we still have those ugly default delete buttons.

So now lets start customizing!  First, we are going to need a UITableViewCell subclass, to add one go to File > New File then select ”Objective-C Class” after that go down to the drop-down menu and select “UITableViewCell” now choose a name for the class and click “Finish”.

 

Now that we have our custom cell class, we need to point the tableview to the class. First you need to import the class in the viewcontroller.h.

Second we need to replace these lines in the cellForRowAtIndex method:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];if (cell == nil) {  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];}

with:

className *cell = (className *)[tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];	if (cell == nil) {		cell = [[[className alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];}

We are now pointing the tableview to the custom class.

Now for that last but not least step:

In your custom cell class add the following method:

- (void)willTransitionToState:(UITableViewCellStateMask)state{  [super willTransitionToState:state];  if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {	for (UIView *subview in self.subviews) {	  if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {             		UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];		[deleteBtn setImage:[UIImage imageNamed:@"delete.png"]];		[[subview.subviews objectAtIndex:0] addSubview:deleteBtn];		[deleteBtn release];	  }	}   } }

“delete.png” is the name of the custom image, make sure the image is 64 x 33 pixels. This is the result:

 

 

 

 

转载于:https://www.cnblogs.com/simonshi2012/archive/2012/04/09/2438415.html

你可能感兴趣的文章
CNN 笔记
查看>>
版本更新
查看>>
SQL 单引号转义
查看>>
start
查看>>
实现手机扫描二维码页面登录,类似web微信-第三篇,手机客户端
查看>>
PHP socket客户端长连接
查看>>
7、shell函数
查看>>
【转】Apache Jmeter发送post请求
查看>>
Nginx 基本 安装..
查看>>
【凸优化】保留凸性的几个方式(交集、仿射变换、投影、线性分式变换)
查看>>
在没装VS2010的机器上运行VS2010开发的C++程序
查看>>
[Oracle Notes]About Oracle parallel insert performance-有关oracle并行插入性能
查看>>
[Luogu 2805] NOI2009 植物大战僵尸
查看>>
【Python】多线程2
查看>>
【spark】with mongodb
查看>>
矩阵取数游戏
查看>>
php 操作数组 (合并,拆分,追加,查找,删除等)
查看>>
Java 从无类型参数Map到有类型参数Map传值的一个问题
查看>>
css常用的阴影
查看>>
linux上安装mysql
查看>>