头文件:
#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。其他控件的自定义与此类似。
QT自定义窗口 自由拖动 自定义标题
tablayout+viewpager自定义tab和自定义指示器。完美解决滑动卡顿。
用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件用户自定义控件,用户自定义控件,用户自定义控件,用户自定义...
springboot工程通过自定义response注解、java反射机制、自定义java拦截器、自定义功能类实现WebMvcConfigurer接口等功能,实现自定义规范化返回数据结构。
vb.net 自定义控件 自定义属性 UITypeEditor UI 类型编辑器 实例 提供一个示例 UITypeEditor,它使用 IWindowsFormsEditorService 显示用于用户输入的 Form。 IWindowsFormsEditorService 只能通过 PropertyGrid ...
自定义按钮自定义按钮自定义按钮自定义按钮自定义按钮
自定义导航模板
1.包含自定义消息 2.包含Panel类型的自定义插件 3.包含Display类型的自定义插件
这是我用C#写的自定义组件和控件,内附一个testForm来演示如何使用。 其中,自定义的组件有:速选组件、窗口靠边停靠组件、窗口抖动组件。 自定义控件有:黑白的菜单栏、带进度显示的进度条等。
自定义文件格式 自定义文件格式 自定义文件格式 自定义文件格式 自定义文件格式
Android自定义中国象棋,原创-------转载注明出处; Android自定义中国象棋,原创-------转载注明出处; Android自定义中国象棋,原创-------转载注明出处; Android自定义中国象棋,原创-------转载注明出处; ...
FreeSwitch完整的自定义模块定义和改善自定义事件的例子,详细介绍见博客:https://blog.csdn.net/xxm524/article/details/126211171
java自定义标签java自定义标签java自定义标签java自定义标签java自定义标签java自定义标签
自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签...
介绍了mybatis自定义标签,添加自定义标签的实现代码,通过实例代码展示给大家
uni-app自定义弹窗组件指令部分,支持自定义图片,文本、按钮等功能。
JAVA动态表单设计,自定义表单,自定义数据,在线设计,数据库存储
sql自定义函数 sql自定义函数 sql自定义函数
微信小程序源码(含截图)自定义tabbar微信小程序源码(含截图)自定义tabbar微信小程序源码(含截图)自定义tabbar微信小程序源码(含截图)自定义tabbar微信小程序源码(含截图)自定义tabbar微信小程序源码(含...