头文件:
#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
示例图:
分享到:
相关推荐
自定义控件是C#编程中一个重要的概念,它允许开发者根据需求扩展或修改内置控件的功能和外观,以满足特定项目的需求。在本案例中,我们将深入探讨如何自定义ComboBox控件。 ComboBox控件是Windows Forms中一个非常...
在QT编程中,自定义窗口是一项常见的需求,它允许开发者根据应用的需求来设计独特的界面元素和交互方式。本文将深入探讨如何在QT中创建自定义窗口,并实现自由拖动和自定义标题的功能。 首先,我们需要了解QT中的...
"C#自定义控件库"是指使用C#语言编写的、由开发者自定义的控件集合,这些控件可以扩展.NET Framework的标准控件集,为用户提供更丰富的界面元素和功能。自定义控件是软件开发中的一个重要环节,特别是在UI设计和用户...
本教程将深入讲解如何在WinForms中自定义CheckBox控件,以满足特定的界面或功能需求。 首先,自定义CheckBox控件主要是为了扩展其默认功能,比如改变其外观、添加额外的事件处理或者提供更复杂的交互逻辑。在VS2005...
鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例鸿蒙自定义组件实例...
在Android开发中,有时我们可能需要对系统的默认控件进行扩展和自定义,以满足特定的设计需求或功能增强。本文将深入探讨如何自定义`RadioGroup`布局,并通过修改源码来创建一个自定义控件。`RadioGroup`是Android...
本主题将深入探讨如何在Spring Boot工程中通过自定义response注解、利用Java反射机制、设置自定义拦截器以及实现WebMvcConfigurer接口来实现这一目标。 首先,我们来看自定义response注解。在Spring Boot中,可以...
【vue+printJs】前端打印, 自定义字体大小, 自定义样式, 封装共享样式,开箱即用【vue+printJs】前端打印, 自定义字体大小, 自定义样式, 封装共享样式,开箱即用【vue+printJs】前端打印, 自定义字体大小, 自定义样式,...
在C#编程中,自定义用户控件是提高应用程序界面美观性和功能多样性的重要手段。本教程将基于给定的"C#自定义漂亮按钮"主题,深入讲解如何利用C#和Visual Studio 2010创建一个自定义的按钮控件。我们将主要探讨以下几...
在C# WinForm应用开发中,自定义RadioButton控件是一种常见的需求,这通常涉及到扩展.NET Framework提供的默认RadioButton控件的功能,以满足特定的设计或交互需求。本教程将深入讲解如何在Visual Studio 2005及其更...
自定义用户控件是提升应用程序功能和界面个性化的重要手段。在这个场景中,我们关注的是一个特定的自定义控件,即基于PictureBox的扩展。PictureBox是.NET Framework提供的一个标准控件,用于显示图像,而自定义用户...
因此,开发者经常需要自定义日期选择器来提供更符合应用风格或特定功能的交互体验。这篇内容将深入探讨如何在Android中创建一个自定义日期选择器,并通过源码分析来增强我们的理解。 首先,我们要明白自定义日期...
在AutoCAD平台上,开发者可以利用ObjectARX(Autodesk Reactor Extension)库来创建自定义实体,这是一种基于C++的编程接口,允许程序员深入到AutoCAD的内部工作流程,实现扩展功能和定制化操作。本篇文章将详细探讨...
在实际的企业环境中,根据业务需求,我们可能需要对默认的CAS登录页面进行自定义,以提供更符合品牌形象或用户体验的界面。下面将详细讲解如何配置和实现CAS的自定义登录页面。 一、CAS自定义登录页面概述 CAS的...
vb.net 自定义控件 自定义属性 UITypeEditor UI 类型编辑器 实例 提供一个示例 UITypeEditor,它使用 IWindowsFormsEditorService 显示用于用户输入的 Form。 IWindowsFormsEditorService 只能通过 PropertyGrid ...
本篇文章将详细探讨如何在Qt中实现一个自定义的提示框(Tip),即“自定义tip”。 首先,我们要理解“tooltip”在UI设计中的作用。Tooltip是当鼠标悬停在某个控件上时,会短暂显示的一段文字信息,用于提供关于该控件...
在开发这些应用程序时,有时我们需要根据项目需求创建特定的用户界面元素,这正是自定义控件的作用所在。本主题将深入探讨如何在C#中自定义一个类似于系统默认`MessageBox`的控件。 `MessageBox`是.NET Framework...
方便调用,有示例。 Android自定义View图片裁剪,支持自由裁剪、按自定义比例裁剪、圆形裁剪、旋转、镜面翻转,从0到1自定义View。kotlin编写的一个自定义View。
本资料包“C#自定义控件.rar”显然是关于如何在C#环境中创建和使用自定义控件的教程或示例代码。 自定义控件的创建通常分为几个步骤: 1. **基础类选择**:首先,你需要选择一个基础类来继承。这可以是系统提供的...
Winform 程序自定义操作安装、卸载安装包制作详解 Winform 程序自定义操作安装、卸载安装包制作是指在 Winform 程序中实现自定义的安装和卸载操作,包括安装和卸载时的文件处理和保留某些文件等。下面是 Winform ...