`
jsntghf
  • 浏览: 2564284 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

自定义UITableViewCellAccessoryCheckmark

    博客分类:
  • iOS
阅读更多

头文件:

#import <UIKit/UIKit.h>

@interface TableView : UITableViewController {
	NSMutableArray *dataArray;
}

@property (nonatomic, retain) NSMutableArray *dataArray;
@end

 

实现文件:

#import "TableView.h"

@implementation TableView

@synthesize dataArray;

- (void)viewDidLoad
{
	NSString *path = [[NSBundle mainBundle] pathForResource:@"CheckMark" ofType:@"plist"];
	self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [dataArray count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
	[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *kCustomCellID = @"MyCellID";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
	if (cell == nil)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
	}
	
	NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
	cell.textLabel.text = [item objectForKey:@"text"];
	
	[item setObject:cell forKey:@"cell"];
	
	BOOL checked = [[item objectForKey:@"checked"] boolValue];
	UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
	
	UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
	CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
	button.frame = frame;
	
	[button setBackgroundImage:image forState:UIControlStateNormal];
	
	[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
	button.backgroundColor = [UIColor clearColor];
	cell.accessoryView = button;
	
	return cell;
}

- (void)checkButtonTapped:(id)sender event:(id)event
{
	NSSet *touches = [event allTouches];
	UITouch *touch = [touches anyObject];
	CGPoint currentTouchPosition = [touch locationInView:self.tableView];
	NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
	if (indexPath != nil)
	{
		[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
	}
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{	
	NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
	
	BOOL checked = [[item objectForKey:@"checked"] boolValue];
	
	[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
	
	UITableViewCell *cell = [item objectForKey:@"cell"];
	UIButton *button = (UIButton *)cell.accessoryView;
	
	UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
	[button setBackgroundImage:newImage forState:UIControlStateNormal];
}

- (void)viewDidUnload
{
	self.dataArray = nil;
}

- (void)dealloc
{	
    [dataArray release];
	[super dealloc];
}

@end

 

示例图:


分享到:
评论
1 楼 gypgyp 2012-05-10  
Good.善哉!

相关推荐

    C# 自定义控件 自定义ComboBox

    自定义控件是C#编程中一个重要的概念,它允许开发者根据需求扩展或修改内置控件的功能和外观,以满足特定项目的需求。在本案例中,我们将深入探讨如何自定义ComboBox控件。 ComboBox控件是Windows Forms中一个非常...

    QT自定义窗口 自由拖动 自定义标题

    在QT编程中,自定义窗口是一项常见的需求,它允许开发者根据应用的需求来设计独特的界面元素和交互方式。本文将深入探讨如何在QT中创建自定义窗口,并实现自由拖动和自定义标题的功能。 首先,我们需要了解QT中的...

    C#自定义控件库

    "C#自定义控件库"是指使用C#语言编写的、由开发者自定义的控件集合,这些控件可以扩展.NET Framework的标准控件集,为用户提供更丰富的界面元素和功能。自定义控件是软件开发中的一个重要环节,特别是在UI设计和用户...

    C# WinForm 自定义CheckBox

    本教程将深入讲解如何在WinForms中自定义CheckBox控件,以满足特定的界面或功能需求。 首先,自定义CheckBox控件主要是为了扩展其默认功能,比如改变其外观、添加额外的事件处理或者提供更复杂的交互逻辑。在VS2005...

    Qt自定义messageBox弹窗代码

    自定义messageBox通常涉及以下几个方面:界面布局的自定义、按钮的自定义、图标以及按钮的布局等。开发者可能需要重写QMessageBox的默认行为,通过继承QMessageBox类并重载相关的方法来实现定制化的弹窗。 自定义...

    鸿蒙+自定义组件+实例代码

    鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例...

    Android 自定义RadioGroup布局,修改源码自定义控件

    在Android开发中,有时我们可能需要对系统的默认控件进行扩展和自定义,以满足特定的设计需求或功能增强。本文将深入探讨如何自定义`RadioGroup`布局,并通过修改源码来创建一个自定义控件。`RadioGroup`是Android...

    springboot工程自定义response注解、自定义规范化返回数据结构

    本主题将深入探讨如何在Spring Boot工程中通过自定义response注解、利用Java反射机制、设置自定义拦截器以及实现WebMvcConfigurer接口来实现这一目标。 首先,我们来看自定义response注解。在Spring Boot中,可以...

    DataGridView添加自定义Columntype

    ### DataGridView添加自定义ColumnType 在.NET Framework中,DataGridView是一个非常强大的数据展示与编辑控件,广泛应用于Windows Forms应用程序中。为了满足不同的业务需求,我们常常需要对DataGridView进行...

    C#自定义按钮、自定义WinForm无边框窗体、自定义MessageBox窗体

    本例子包含C#自定义按钮、自定义WinForm无边框窗体、自定义MessageBox窗体 三个小例子,具体展现效果可以到:http://www.cnblogs.com/JiYF/p/8686463.html查看

    【vue+printJs】前端打印, 自定义字体大小, 自定义样式, 封装共享样式,开箱即用

    【vue+printJs】前端打印, 自定义字体大小, 自定义样式, 封装共享样式,开箱即用【vue+printJs】前端打印, 自定义字体大小, 自定义样式, 封装共享样式,开箱即用【vue+printJs】前端打印, 自定义字体大小, 自定义样式,...

    c#自定义漂亮按钮

    在C#编程中,自定义用户控件是提高应用程序界面美观性和功能多样性的重要手段。本教程将基于给定的"C#自定义漂亮按钮"主题,深入讲解如何利用C#和Visual Studio 2010创建一个自定义的按钮控件。我们将主要探讨以下几...

    C# WinForm 自定义 RadioButton

    在C# WinForm应用开发中,自定义RadioButton控件是一种常见的需求,这通常涉及到扩展.NET Framework提供的默认RadioButton控件的功能,以满足特定的设计或交互需求。本教程将深入讲解如何在Visual Studio 2005及其更...

    C# 自定义用户控件(PictureBox)

    自定义用户控件是提升应用程序功能和界面个性化的重要手段。在这个场景中,我们关注的是一个特定的自定义控件,即基于PictureBox的扩展。PictureBox是.NET Framework提供的一个标准控件,用于显示图像,而自定义用户...

    Android自定义日期选择器源码

    因此,开发者经常需要自定义日期选择器来提供更符合应用风格或特定功能的交互体验。这篇内容将深入探讨如何在Android中创建一个自定义日期选择器,并通过源码分析来增强我们的理解。 首先,我们要明白自定义日期...

    cas 自定义登录页面

    在实际的企业环境中,根据业务需求,我们可能需要对默认的CAS登录页面进行自定义,以提供更符合品牌形象或用户体验的界面。下面将详细讲解如何配置和实现CAS的自定义登录页面。 一、CAS自定义登录页面概述 CAS的...

    android 自定义view及自定义属性

    在Android开发中,自定义View和自定义属性是提升应用个性化和功能扩展性的重要手段。本文将深入探讨这两个核心概念,以及如何在实际项目中应用它们。 ### 自定义View 自定义View允许开发者创建自己的视图组件,以...

    Arx创建自定义实体

    在AutoCAD平台上,开发者可以利用ObjectARX(Autodesk Reactor Extension)库来创建自定义实体,这是一种基于C++的编程接口,允许程序员深入到AutoCAD的内部工作流程,实现扩展功能和定制化操作。本篇文章将详细探讨...

    自定义的LinearLayout使用自定义的View

    在Android开发中,自定义组件是一项重要的技能,它允许开发者根据特定需求打造独特的用户界面。本主题主要关注如何在自定义的LinearLayout中嵌入自定义的View,并利用GridView来有效地组织这些视图。 首先,我们要...

    Qt实现的自定义tip

    本篇文章将详细探讨如何在Qt中实现一个自定义的提示框(Tip),即“自定义tip”。 首先,我们要理解“tooltip”在UI设计中的作用。Tooltip是当鼠标悬停在某个控件上时,会短暂显示的一段文字信息,用于提供关于该控件...

Global site tag (gtag.js) - Google Analytics